Fork me on GitHub

直接上源码:

import os
import time


# 定义函数名:在py文件路径下创建cache的txt文件
def txt(name, text):
    # os.getcwd() 获取当前的工作路径;
    new = os.getcwd() + '/cache/'
    # 判断当前路径是否存在,没有则创建new文件夹
    if not os.path.exists(new):
        os.makedirs(new)

    # 在当前py文件所在路径下的new文件中创建txt
    xxoo = new + name

    # 打开文件,open()函数用于打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写。
    file = open(xxoo, 'w')

    # 写入内容信息
    file.write(text)

    file.close()
    print('文件创建成功', xxoo)

timer = time.strftime("%H_%M_%S")
file_name = timer+'.txt'
txt(file_name, 'hello,world')

 

posted on 2023-06-08 17:12  虚生  阅读(42)  评论(0编辑  收藏  举报