区域:IOError:[Errno 22]无效模式('w')或filenam

2024-10-06 16:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么,但出于某种原因,每当输出文件的文件名中有“region”时,就会出现以下错误:

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”,但它总是出现这个错误。有办法绕过吗?


Tags: 文件pathnameselftxtoutputhere文件名
3条回答

此外,当尝试从SharePoint共享驱动器打开文件>;50 MB时,Python还会给出此消息。

https://support.microsoft.com/en-us/help/2668751/you-cannot-download-more-than-50-mb-or-upload-large-files-when-the-upl

在C标准语言中,\t\n\r是转义字符。\t是下一个制表位的横线。\n是换行符,\r是回车符。你应该使用\\r/r,你就能解决问题!

使用正斜杠:

'path/regionlog.txt'

或原始字符串:

r'path\regionlog.txt'

或者至少摆脱你的反睫毛:

'path\\regionlog.txt'

\r是回车。


另一个选择是:使用os.path.join,您根本不必担心斜杠:

output = os.path.abspath(os.path.join('path', 'regionlog.txt'))

相关问题 更多 >