file open fails.

A

Atul.

Hi I am using IDLE on Windows Vista and I have a small code.

title = 'C:\Thesis\refined_title.txt'
file = open(title)

I get the following error from Python.


file = open(title)
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Thesis
\refined_title.txt'

Now, I can not understand the problem here I have all the permissions
set for the folder, file as well. I can not understand why would it
happen. Is it known on Windows Vista or am I missing something really
simple and stupid? Please help.

Regards,
Atul.
 
M

MRAB

Atul. said:
Hi I am using IDLE on Windows Vista and I have a small code.

title = 'C:\Thesis\refined_title.txt'
file = open(title)

I get the following error from Python.


file = open(title)
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Thesis
\refined_title.txt'

Now, I can not understand the problem here I have all the permissions
set for the folder, file as well. I can not understand why would it
happen. Is it known on Windows Vista or am I missing something really
simple and stupid? Please help.
Backslash has a special meaning in string literals. Use a raw string
instead or double the backslashes:

title = r'C:\Thesis\refined_title.txt'

or:

title = 'C:\\Thesis\\refined_title.txt'
 
W

Wes James

Atul. wrote:

In your case, '\r' is a return (a single character), not two
characters long. I think its sad that 'C:\Thesis' doesn't cause
an error because there is no such character as '\T', but I am
probably excessively pedantic.

\T might mean the same thing as \t (tab), but I thought it would be different...

-wj
 
S

Steven D'Aprano

I guess not:

http://docs.python.org/reference/lexical_analysis.html#string-literals

Wonder why when I do print "test\Ttest" vs print "test\ttest" \T just
get printed?


Did you read the section you just linked to? It says so right there:

"Unlike Standard C, all unrecognized escape sequences are left in the
string unchanged, i.e., the backslash is left in the string. (This
behavior is useful when debugging: if an escape sequence is mistyped, the
resulting output is more easily recognized as broken.) It is also
important to note that the escape sequences marked as “(Unicode only)†in
the table above fall into the category of unrecognized escapes for non-
Unicode string literals."

Since there is no standard escape \T then it gets treated as a literal
backslash + uppercase t.
 
A

afriere

Wouldn't it be easier just to avoid the windows slashes altogether and
stick to the posix:

title = 'c:/thesis/refined_title.txt'
 
A

alex23

Well, yes in a way, but lots of places in windows you can get a copy
of a full file spec into the cut buffer, so you cut/paste rather than
type.  If you do that, you get the backslashes (and you better be
prepared for that by using r'' to paste into).

The obvious solution, then, is to write something that monitors the
clipboard, intercepts any strings that are file paths, and replaces
the backslashes with their forward counterpart...

:)
 
J

John Machin

The obvious solution, then, is to write something that monitors the
clipboard, intercepts any strings that are file paths, and replaces
the backslashes with their forward counterpart...

:)

You would need to monitor where the user was about to paste it --
forward slashes in file paths are not grokked well at all in a Command
Prompt window :-S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top