[Tutor] File reading problem

Jeff Shannon jeff@ccvcorp.com
Mon Jul 21 16:08:08 2003


DORSEY_EDMUND_K@LILLY.COM wrote:

>
> I'm trying to read in some files and for some reason python keeps 
> telling me  IOError: [Errno 2] No such file or directory: 
> 'c:\\models\tiny.vff' 


You need to escape your backslashes.  '\t' is a tab, not a backslash and 
the letter 't'.  It works with the super_tiny.vff file, because \s (like 
\m) happens to not indicate a control character.

You can fix this by changing this line (and any other lines that use 
filenames):

> reader.readFile(filename = "c:\models\tiny.vff")


to look like either one of the following:

reader.readFile(filename = "c:\\models\\tiny.vff")
reader.readFile(filename = r"c:\models\tiny.vff")
reader.readFile(filename = "c:/models/tiny.vff")

In the first one, I've used double backslashes.  This indicates that the 
backslash should be interpreted literally, rather than potentially 
interpreted as a control character.

In the second one, I've preceded the string with an 'r', which indicates 
that this is to be a raw string.  In raw strings, no control character 
interpretation is done.

In the last case, I've used forward slashes instead of backslashes. 
 This isn't Windows-standard, but it will work for (AFAIK) every system 
call that's passed through Python.  (It doesn't work in the Windows 
command shell, but it works pretty much everywhere else.)

You can also import the os module and use os.path.join() to construct 
your filename.

Jeff Shannon
Technician/Programmer
Credit International