File Handling Problems Python I/O

J

Josh

Hi,

I am having a problem with Python. I am new to Python as a programming
language, but I do have experience in other languages. I am
experiencing strange problems with File handling and wonder if anyone
else has seen this or knows what I am doing wrong. I am simply trying
to open a file for read and iterate through until the end of file. I am
able to do so without a problem, but here's the catch: The open
statement is only working on certain files. I open a simple text file
say file1.txt without any issues, but I change the open statement to
another text file and it error's out stating the file doesn't exist. I
know the code is correct because it worked for the other file. I have
tried both binary and ascii modes to no avail. Any idea why this is
happening? I am running Python 2.3.4 wxPython 2.5.3.1 and SPE as the
IDE on Win2k. Thanks

Josh
 
A

Arjen Dijkstra

I don't think we can help if you don't post some of your code.

Regards,
Arjen
 
P

Peter Hansen

Josh said:
I am having a problem with Python. I am new to Python as a programming
language, but I do have experience in other languages. I am
experiencing strange problems with File handling and wonder if anyone
else has seen this or knows what I am doing wrong. I am simply trying
to open a file for read and iterate through until the end of file. I am
able to do so without a problem, but here's the catch: The open
statement is only working on certain files. I open a simple text file
say file1.txt without any issues, but I change the open statement to
another text file and it error's out stating the file doesn't exist.

The usual rookie mistake here is to fail to recognize
that backslashes in strings are interpreted as special
escape sequences if they precede certain characters such
as "t" or "b". You probably have a path that looks like
this: "c:\temp\myfile.txt", right? The \t is actually
being converted into a TAB character.

You can use *forward* slashes in most cases in Windows,
except on the command line. The simplest fix is just
to convert your backslashes to forward slashes:
"c:/temp/myfile.txt"

Another approach, somewhat less desirable, is to use
"raw" strings instead, to prevent the escape sequences from
being recognized:
r"c:\temp\myfile.txt"

Finally, and by far least desirable, use double-backslashes
to escape each backslash and in effect nullify the usual
escape handling:
"c:\\temp\\myfile.txt"

Much less readable that way, but sometimes the right thing to do...

-Peter
 
J

Josh

He is the function where I am making the call. If I change the open
statment to another file, say "c:\test.txt", a file I know exists, it
will error out stating the file does not exist. Thanks

Josh

def GetStartVars(self):
try:
DOWNFILE = open("c:\fixes.txt","r")
except IOError:
print "Failed to open file."
else:
counter = 1
while 1:
theline = DOWNFILE.readline()
if not theline:
break
print theline
counter = counter + 1
print counter
DOWNFILE.close()
 
J

Josh

Peter,

Thank you for the rookie correction. That was my exact problem. I
changed the address to use forward slashes and it works perfect. I did
not know that a backslash had special meaning within a string, but now
I do! Thanks again

Josh
 
A

Alex Martelli

Josh said:
He is the function where I am making the call. If I change the open
statment to another file, say "c:\test.txt", a file I know exists, it

Are you sure a file exist whose name is, c, colon, tab, e, s, t ...?

\t is an escape sequence and it means TAB (character of ascii code 9).

Try 'c:/test.txt' -- using normal slash instead the backslash -- and you
should be fine.

You mention you have some experience with other languages: if those
languages include C, or Java, or C++, and many others besides (all which
use backslashes for escape sequences in strings), you'd have exactly the
same issues.


Alex
 
M

mhartl

You can use the os module to build path names in a platform-independent
manner. On my Linux box, I can type
'/foo/bar/baz'

On a Windows machine, you get
'C:\\foo\\bar\\baz'

This is a little more cumbersome that just typing the string, but using
os.path.join makes it easy to port your scripts to a new platform.
Check out http://docs.python.org/lib/module-os.path.html for more
information.
Cheers,

Michael
 
J

Josh

Micheal,

Thanks for the advice as the programming I am doing will be run on both
Windows and Linux based PC's, that being the main reason for my venture
into Python. I'm glad to see that people are willing to help out even
the newbie's.

Josh
 
P

Peter Hansen

Josh said:
He is the function where I am making the call. If I change the open
statment to another file, say "c:\test.txt", a file I know exists, it
will error out stating the file does not exist. Thanks

def GetStartVars(self):
try:
DOWNFILE = open("c:\fixes.txt","r")

Josh, it's surprising that this example worked for you,
given that \f is another string escape (in this case
it's an ASCII FF (formfeed), character code 12, instead
of the TAB (code 8) character that c:\test.txt gives you.

Are you sure this was one of the names that worked?

BTW, the following link in the docs provides a list of
all the recognized escape sequences:

http://www.python.org/doc/current/ref/strings.html

-Peter
 
C

Craig Ringer

Peter,

Thank you for the rookie correction. That was my exact problem. I
changed the address to use forward slashes and it works perfect. I did
not know that a backslash had special meaning within a string, but now
I do! Thanks again

There's another common mistake you might want to head off now, too.
Consider the following program:

--------
directory = r"c:\mydata"
datafilename = "something.txt"

datafile = open(directory + datafilename,"r")
---------

It's very common for this to happen, usually when the paths are
originally written with trailing slashes then changed to not have them
later.

Rather than saying "all paths must have trailing slashes", manually
formatting in slashes, or other platform-specific uglyness, consider
using the os.path module to take care of it:

--------
import os

directory = r"c:\mydata"
datafilename = "something.txt"

datafile = open(os.path.join(directory,datafilename),"r")
--------

This will work on any platform (so long as the literal paths are
correct) and will work no matter whether or not there are trailing path
separators on the input strings. os.path.join can take more than two
arguments, too.

os.path has lots of other handy tools, so I strongly recommend checking
it out.
 

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,215
Messages
2,571,113
Members
47,710
Latest member
HarleyMoli

Latest Threads

Top