A silly question on file opening

J

joy99

Dear Group,

I was using Python with IDLE as GUI for quite some time. My Operating
System was Windows XP with Service Pack2.
Recently I changed the Operating System to Windows XP with Service
Pack3. I had to reinstall Python for which I downloaded
"python-2.6.4.msi"and loaded it in my D drive.

Here, as I am trying to open the file if I am giving a statement like,
file_open=open("python26/file","r")
It is showing an error,
On checking how the files are stored it is showing that it is storing
in D drive directly but not in Python folder in D drive there is no
Python folder at all.
I tried to change the location to D:\file and as I saw in Python Docs
the file reading option is now "r+" so I changed the statement to
file_open=open("D:\file","r+")
but it is still giving error.
Did I install it wrongly?
If you can please help it out.
Best Regards,
Subhabrata.
 
A

Anthony Tolle

Dear Group,
[snip]
I tried to change the location to D:\file and as I saw in Python Docs
the file reading option is now "r+" so I changed the statement to
   file_open=open("D:\file","r+")
but it is still giving error.

Only use "r+" if you need to also write to the file. "r" is still
good for opening for reading.

Without seeing a traceback, I can only assume the error is caused by
using a backslash in the path without escaping it. Try either the
following:

file_open=open("D:\\file","r+")

file_open=open(r"D:\file","r+")

For an explanation, see the Python documentation:

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

Steven D'Aprano

I tried to change the location to D:\file and as I saw in Python Docs
the file reading option is now "r+" so I changed the statement to
file_open=open("D:\file","r+")
but it is still giving error.


You should copy and paste (do not re-type!) the *exact* line that leads
to an error, and the full traceback that Python prints.

But in this case I can guess what the problem is. Like most C-inspired
languages, Python uses backslashes to put in special characters: e.g. \n
for newline, \t for tab, and \f for formfeed.

So when you try to open "D:\file" you are actually looking for a file on
D drive called (chr(12) + "ile"), not "file".

The solution to this is to remember that Windows accepts forward slashes
as well as backslashes, and always use the forward slash. So try:

open("D:/file")

and see if that works.

D:
ileD:/file
 
N

Nobody

The solution to this is to remember that Windows accepts forward slashes
as well as backslashes, and always use the forward slash. So try:

open("D:/file")

and see if that works.

The solution is not to hard-code pathnames in your source file. Read the
base directory from an environment variable, registry key, or
configuration file, and use os.path.join() to construct paths relative to
that directory.

On Unix, there are situations which require hard-coding pathnames, e.g.
the path to the package's system-wide configuration file.

But on Windows, there isn't a single directory whose name is fixed.
"Windows" isn't guaranteed to be on "C:", and the other standard
directories typically have names which vary by language. Hard-coding
"Program Files" is a quick way to guarantee that your software won't work
on systems using a language other than English.
 
J

joy99

Dear Group,
[snip]
I tried to change the location to D:\file and as I saw in Python Docs
the file reading option is now "r+" so I changed the statement to
   file_open=open("D:\file","r+")
but it is still giving error.

Only use "r+" if you need to also write to the file.  "r" is still
good for opening for reading.

Without seeing a traceback, I can only assume the error is caused by
using a backslash in the path without escaping it.  Try either the
following:

file_open=open("D:\\file","r+")

file_open=open(r"D:\file","r+")

For an explanation, see the Python documentation:

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

Dear Group,

I am sorry I could not report. I was trying your solutions, I could
try only one or two, they are working. My problem got solved. Thank
you for your suggestions. But soon I find time, I would try all of
them, so that I can learn more and what I try to do in this room to
get better angles over any problem from experts like you. Thank you
for giving me your valuable time despite your busy schedule.

Wishing you happy day ahead,
Best Regards,
Subhabrata.
 

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
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top