如何在创建文件夹的同时创建文件

import os
import time

folder_date = time.strftime(‘%Y-%m-%d’, time.localtime())
file_date = time.strftime(‘%Y-%m-%d_%H-%M-%S’, time.localtime())

path = ‘D:/{}’.format(folder_date)

folder = os.mkdir(path)

file = ‘{folder}/{file_date}”.txt”‘ # 在创建以时间命名的文件夹下创建以时间命名的.txt文件

record = open(file, ‘a’)

运行以上代码,只能创建文件夹,无法在其下面创建.txt文件,请大佬们帮忙看看是哪里的问题,感谢!

Jason990420
最佳答案

未呼叫文件的 close 方法 !

  • 数据丢失,缓冲区没有满之前不会写入文件中。
  • 文件句柄被占用,多线程或者进程情况下 会造成文件写入错乱,造成脏数据。
  • 对象未解除引用,垃圾回收无法回收此对象占用。
>>> import os
>>> import time
>>> folder_date = time.strftime('%Y-%m-%d', time.localtime())
>>> folder_date
'2022-05-23'
>>> file_date = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
>>> file_date
'2022-05-23_18-20-11'
>>> path = 'D:/{}'.format(folder_date)
>>> path
'D:/2022-05-23'
>>> folder = os.mkdir(path)             # 错误: 返回值为 None
>>> repr(folder)
'None'
>>> file = '{folder}/{file_date}".txt"' # 错误: 变量不会代入
>>> file
'{folder}/{file_date}".txt"'
>>> file = f'{folder}/{file_date}.txt'
>>> file
'None/2022-05-23_18-20-11.txt'
>>> record = open(file, 'a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'None/2022-05-23_18-20-11.txt'
>>> file = f'{path}/{file_date}.txt'
>>> file
'D:/2022-05-23/2022-05-23_18-20-11.txt'
>>> record = open(file, 'a')
>>> record.close()

Example

>>> import os
>>> import time
>>> folder_date = time.strftime('%Y-%m-%d', time.localtime())
>>> file_date = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
>>> path = f'd:/{folder_date}'
>>> os.mkdir(path)
>>> file = f'{path}/{file_date}.txt'
>>> file
'd:/2022-05-23/2022-05-23_18-28-14.txt'
>>> record = open(file, 'a')
>>> record.close()
1年前 评论
Jacen (楼主) 1年前
Jason990420 (作者) 1年前
Jacen (楼主) 1年前
讨论数量: 4
Jason990420

未呼叫文件的 close 方法 !

  • 数据丢失,缓冲区没有满之前不会写入文件中。
  • 文件句柄被占用,多线程或者进程情况下 会造成文件写入错乱,造成脏数据。
  • 对象未解除引用,垃圾回收无法回收此对象占用。
>>> import os
>>> import time
>>> folder_date = time.strftime('%Y-%m-%d', time.localtime())
>>> folder_date
'2022-05-23'
>>> file_date = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
>>> file_date
'2022-05-23_18-20-11'
>>> path = 'D:/{}'.format(folder_date)
>>> path
'D:/2022-05-23'
>>> folder = os.mkdir(path)             # 错误: 返回值为 None
>>> repr(folder)
'None'
>>> file = '{folder}/{file_date}".txt"' # 错误: 变量不会代入
>>> file
'{folder}/{file_date}".txt"'
>>> file = f'{folder}/{file_date}.txt'
>>> file
'None/2022-05-23_18-20-11.txt'
>>> record = open(file, 'a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'None/2022-05-23_18-20-11.txt'
>>> file = f'{path}/{file_date}.txt'
>>> file
'D:/2022-05-23/2022-05-23_18-20-11.txt'
>>> record = open(file, 'a')
>>> record.close()

Example

>>> import os
>>> import time
>>> folder_date = time.strftime('%Y-%m-%d', time.localtime())
>>> file_date = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
>>> path = f'd:/{folder_date}'
>>> os.mkdir(path)
>>> file = f'{path}/{file_date}.txt'
>>> file
'd:/2022-05-23/2022-05-23_18-28-14.txt'
>>> record = open(file, 'a')
>>> record.close()
1年前 评论
Jacen (楼主) 1年前
Jason990420 (作者) 1年前
Jacen (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!