Reputation: 449
i'm using pyton 2.7. I have written a script , when it is executed, it will call and run some other file with the name abc.py. but i'm getting error
IOError: [Errno 22] invalid mode ('r') or filename: 'F:\x07bc.c'
it is working fine if i change the file name. it shows error only if i use letters from a to f as the first letter of file name Please help. Thank you
Upvotes: 0
Views: 3989
Reputation: 212835
Try this:
open(r'F:\abc.c')
i.e. add r
before quotes.
UPDATE Sorry, I misinterpreted the code (although my solution is correct). @DavidHeffernan is right, the \a
is read as the ASCII bell.
Upvotes: 1
Reputation: 612874
Consider the Python string '\a'
. As described in the documentation, the back slash character is interpreted as an escape character. So '\a'
is in fact the ASCII Bell character, character number 7.
Your filename is 'F:\abc.c'
and the \a
in there is interpreted as ASCII Bell. You can see this clearly in the interpretor:
>>> 'F:\abc.c'
'F:\x07bc.c'
>>> print 'F:\abc.c'
F:bc.c
When you print that string note that the \a
does not appear. That's because it has been turned into a Bell control character which is invisible.
To include a backslash you can use the correct escape sequence \\
. Put it all together and your filename should be: 'F:\\abc.c'
. As an alternative, you can prefix the string with r
to make it a raw string. This is also detailed in the documentation.
>>> 'F:\\abc.c'
'F:\\abc.c'
>>> print 'F:\\abc.c'
F:\abc.c
>>> r'F:\abc.c'
'F:\\abc.c'
>>> print r'F:\abc.c'
F:\abc.c
Upvotes: 5
Reputation: 4348
Escape the \
with another backslash, like this:
print 'F:\\x07bc.c'
Upvotes: 1