By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

File Handling Problems Python I/O

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
I don't think we can help if you don't post some of your code.

Regards,
Arjen

Josh wrote:
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

Jul 18 '05 #2

P: n/a
Josh wrote:
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
Jul 18 '05 #3

P: n/a
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()

Jul 18 '05 #4

P: n/a
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

Jul 18 '05 #5

P: n/a
Josh wrote:
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

you may want to check "python gotchas" to avoid
other typical newbie errors:

<http://www.ferg.org/projects/python_gotchas.html>

HTH,
deelan

--
"Vedrai come ti tratteranno le mie TV"
Berlusconi a Follini (Luglio 2004)
Jul 18 '05 #6

P: n/a
Josh <Jo**********@gmail.com> wrote:
...
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
Jul 18 '05 #7

P: n/a
You can use the os module to build path names in a platform-independent
manner. On my Linux box, I can type
BASE = '/'
import os
os.path.join(BASE, 'foo', 'bar', 'baz') '/foo/bar/baz'

On a Windows machine, you get
BASE = 'C:'
import os
os.path.join(BASE, 'foo', 'bar', 'baz')

'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

--
Michael Hartl
http://michaelhartl.org/

Jul 18 '05 #8

P: n/a
Sorry, the BASE variable should be 'C:\\' on Windows:
BASE = 'C:\\'
import os
os.path.join(BASE, 'foo', 'bar', 'baz')

'C:\\foo\\bar\\baz'

Jul 18 '05 #9

P: n/a
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

Jul 18 '05 #10

P: n/a
Josh wrote:
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
Jul 18 '05 #11

P: n/a
On Fri, 2005-01-07 at 02:06, Josh wrote:
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.

--
Craig Ringer

Jul 18 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.