Python 使用文件写入期间的时间输出文件
我想使用文件写入时的时间作为文件名来写入文件Python 使用文件写入期间的时间输出文件,python,Python,我想使用文件写入时的时间作为文件名来写入文件 import time, datetime current_time = time.time() endtime = current_time + 12*60*60 while current_time < endtime: file_time = time.strftime('%Y-%m-%d_%H:%M:%S') outfile = open ('outfile_{}.txt'.format(file_time)
import time, datetime
current_time = time.time()
endtime = current_time + 12*60*60
while current_time < endtime:
file_time = time.strftime('%Y-%m-%d_%H:%M:%S')
outfile = open ('outfile_{}.txt'.format(file_time),'w')
outfile.close()
time.sleep(30)
导入时间、日期时间
当前时间=time.time()
结束时间=当前时间+12*60*60
当前时间<结束时间:
文件\u time=time.strftime(“%Y-%m-%d\u%H:%m:%S”)
outfile=open('outfile{}.txt'。格式(文件时间),'w')
outfile.close()
时间。睡眠(30)
但是,文件保存时间有问题。如何定义时间
发件人:
感谢您的努力,但我仍然收到以下错误:IOError:[Errno 22]无效模式('w')或文件名:“outfile_2014-03-02_16:36:13.txt”
循环时也分配当前_时间:
# ...
file_time = time.strftime('%Y-%m-%d_%H %M %S')
outfile = open ('outfile_%s.txt' % (file_time),'w')
outfile.close()
time.sleep(30)
current_time = time.time()
你有错误吗?如果是这样的话,您介意将其添加到您的问题中吗?谢谢您的努力,但我仍然得到以下错误:IOError:[Errno 22]无效模式('w'))或者文件名:“outfile_2014-03-02_16:36:13.txt”@rockpy也许您的操作系统不允许在文件名中使用
:
。@AshwiniChaudhary会说:-:
在FAT/NTFS:)中是无效字符@JonClements更具体,所以+1:-)确定,然后将时间戳格式更改为文件\u time=time.strftime(“%Y-%m-%d\u%H%m%S”)
,因为操作系统不允许文件名使用:
。