1

I am trying to write and save a file but the following error is raised by the python script:

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.

3
  • That isn't a valid file name. Commented May 14, 2015 at 13:09
  • 1
    @Ajay - You can certainly use / in a pathname in Windows in a Python script. Commented May 14, 2015 at 13:19
  • 1
    @Ajay: Windows can cope with forward slashes in pathnames, but it has problems with colons. Also using backslashes as path separators brings its own problems. Backslashes in a plain string should be backslash-escaped. Alternatively, the path can be given as a raw string, although a raw string can't have a backslash as the final char. Commented May 14, 2015 at 13:21

2 Answers 2

5

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). There is a bit more information here.

Sign up to request clarification or add additional context in comments.

1 Comment

Solved. Windows... That was all. Thanks
0

In Windows, you can have a colon immediately after the volume name (C:), but not anywhere else in the path. 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
...
>>>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.