我为每个日期创建一个文件夹,然后在该文件夹下创建一组文件。我正在为此使用此功能:
path_stats= pathlib.Path('/home/dataset/Signal_Synchronization_Traces/' + str(date.today()) + '_Results/Statistical_Test_Results').mkdir(parents=True, exist_ok=True)
print(path_stats)
for i in range(10):
file = open(str(path_stats)+'/File'+ str(i) + '.txt','wb')
file.write('hello')
print('done')
file.close
但是,这个函数会导致错误:
file = open(str(path_stats)+'/File'+ str(i) + '.txt','wb')
FileNotFoundError: [Errno 2] No such file or directory: 'None/File0.txt'
None
请您参考如下方法:
在尝试打开文件之前创建文件夹结构:
import os
for i in range(10):
filename = str(path_stats)+'/File'+ str(i) + '.txt'
# Create folder structure
os.makedirs(os.path.dirname(filename), exist_ok=True)
file = open(filename,'wb')
file.write('hello')
print('done')
file.close
请注意,当您以二进制模式打开文件时,您将无法写入字符串“hello”。
