IOError:[Errno 22]无效模式('w')或文件名

Ser*_*tor 6 python errno python-2.7

尝试制作文件时,我收到此错误.它被设计为获取创建的.csv文件并将其放入纯文本文件中.

我希望它在使用日期和时间戳运行后创建一个新文件,但我似乎在尝试生成文件时得到了Errno 22.

有任何想法吗?

import csv
import time

f = open(raw_input('Enter file name: '),"r")

saveFile = open ('Bursarcodes_'+time.strftime("%x")+ '_'+time.strftime("%X")+
                 '.txt', 'w+')

csv_f = csv.reader(f)

for row in csv_f:
    saveFile.write( 'insert into bursarcode_lookup(bursarcode, note_id)' +
                    ' values (\'' + row[0] + '\', ' + row[1] + ')\n')

f.close()

saveFile.close()
Run Code Online (Sandbox Code Playgroud)

And*_*nai 6

您不能在文件名中包含斜杠(/)和冒号(:但在Unix中允许),但它们正是strftime在其输出中生成的斜杠.

Python试图帮助你,它说:

No such file or directory: 'Bursarcodes_01/09/15_19:59:24.txt'
Run Code Online (Sandbox Code Playgroud)

替换time.strftime("%x")为:

time.strftime("%x").replace('/', '.')
Run Code Online (Sandbox Code Playgroud)

......和time.strftime("%X")这个:

time.strftime("%X").replace(':', '_')
Run Code Online (Sandbox Code Playgroud)