简体   繁体   English

Python保存文件

[英]Python saving file

I am trying to write and save a file but the following error is raised by the python script: 我正在尝试编写和保存文件,但是python脚本引发了以下错误:

OSError: [Errno 22] Invalid argument: 'C:/Export/ixxxx/izzzzz_2015-05-12 17:00:00.shp

What is wrong with the path? 路径有什么问题? The directory exists. 该目录存在。

You aren't allowed to use colons in file names in Windows (which I'm guessing is what you're using given the rest of the path). 在Windows中,不允许在文件名中使用冒号(我猜这是在其余路径下使用的冒号)。 There is a bit more information here . 还有更多的信息在这里

In Windows, you can have a colon immediately after the volume name ( C: ), but not anywhere else in the path. 在Windows中,卷名( C: :)后面可以有一个冒号,但路径中的其他任何地方都不能带有冒号。 You'll need to replace the colons with another character. 您需要将冒号替换为另一个字符。 I would use the - character, to keep it consistent with your date format. 我会使用-字符,以使其与您的日期格式保持一致。 As a matter of personal preference, I would probably also replace the space in the filename with a - . 随着个人喜好的问题,我可能也与文件名替换空间-

See the following example: 请参见以下示例:

>>> pathname = 'C:/Temp'  # Change this to your pathname.
>>> filename = 'izzzzz_2015-05-12 17:00:00.shp'
>>> filename = filename.replace(':', '-').replace(' ', '-')
>>> print('{}/{}'.format(pathname, filename))
C:/Temp/izzzzz_2015-05-12-17-00-00.shp
>>> with open('{}/{}'.format(pathname, filename), 'w') as f:
...     pass
...
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM