区域:IOError:[Errno 22] 无效模式('w')或文件名 [英] Region: IOError: [Errno 22] invalid mode ('w') or filename
本文介绍了区域:IOError:[Errno 22] 无效模式('w')或文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道为什么,但出于某种原因,每当我在输出文件的文件名中有区域"时,它都会给我这个错误:
I'm not sure why, but for some reason, whenever I have "region" in the file name of the output file, it gives me this error:
IOError: [Errno 22] 无效模式 ('w') 或文件名: 'path\regionlog.txt'
它为"region.txt"、"logregion.txt"等执行此操作
class writeTo:
def __init__(self, stdout, name):
self.stdout = stdout
self.log = file(name, 'w') #here is where it says the error occurs
output = os.path.abspath('path\regionlog.txt')
writer = writeTo(sys.stdout, output) #and here too
这是为什么?我真的很想将我的文件命名为regionlog.txt",但它不断出现该错误.有办法解决吗?
Why is this? I really would like to name my file "regionlog.txt" but it keeps coming up with that error. Is there a way around it?
推荐答案
使用正斜杠:
'path/regionlog.txt'
或原始字符串:
r'path\regionlog.txt'
或者至少避开你的反斜杠:
Or at least escape your backslashes:
'path\\regionlog.txt'
<小时>
\r
是回车.
另一种选择:使用 os.path.join
并且您根本不必担心斜线:
Another option: use os.path.join
and you won't have to worry about slashes at all:
output = os.path.abspath(os.path.join('path', 'regionlog.txt'))
这篇关于区域:IOError:[Errno 22] 无效模式('w')或文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文