tags:

views:

65

answers:

6

This is the error I am getting:

Traceback (most recent call last):
  File "E:\stuff\module.py", line 91, in <module>
    f = open('E:/stuff/log.txt')
IOError: [Errno 2] No such file or directory: 'E:/stuff/log.txt'

And this is my code:

f = open('E:/stuff/log.txt')

The E:/stuff/log.txt file exists. I can navigate in Windows Explorer and open it so why can't I open it?

EDIT:

Output of DIR command:

C:\Documents and Settings\Administrator>dir e:\stuff
 Volume in drive E has no label.
 Volume Serial Number is 5660-4957

 Directory of e:\stuff

23. 10. 2010  09:26    <DIR>          .
23. 10. 2010  09:26    <DIR>          ..
19. 10. 2010  20:07               385 index.py
23. 10. 2010  16:12             1 954 module.py
22. 10. 2010  19:16             8 335 backprop.py
19. 10. 2010  20:54             1 307 backprop-input.gif
19. 10. 2010  01:48               310 HelloWorld.kpf
23. 10. 2010  15:47                 0 log.txt.txt
               6 File(s)         12 291 bytes
               2 Dir(s)   8 795 586 560 bytes free



C:\Documents and Settings\Administrator>dir e:\
 Volume in drive E has no label.
 Volume Serial Number is 5660-4957

 Directory of e:\

16. 10. 2010  13:32    <DIR>          development-tools
23. 10. 2010  09:26    <DIR>          stuff
               0 File(s)              0 bytes
               2 Dir(s)   8 795 586 560 bytes free

I am running the python script from the cmd like this:

python E:\stuff\module.py
+1  A: 

how about reading permissions? maybe not authorized to read (default mode of open)

tutuDajuju
There are reading and writing permissions.
Richard Knop
+1  A: 

Since it is windows, and the backslash is a escape character, you must double the backslash to escape it. Try

e:\\stuff\\log.txt
Rawheiser
Why not use r"e:\stuff\log.txt"? Why not use `os.path.join( "E:", "stuff", "log.txt" )`?
S.Lott
OP is using forward slashes - escaping is _not_ an issue here. As brady and Tim rightly point out, the `log.txt` file does _not_ exist, it's `log.txt.txt`.
paxdiablo
+1  A: 

it been a long time that i didn't use windows, but if i remember well windows use back-slash in system path so you should do:

import os

file_name = os.path.join("e:\\stuff", "log.txt")

f = open(file_name)

and not:

f = open('E:/stuff/log.txt')

there is not / in paths in windows.

singularity
Python does actually handle forward slashes okay - it may not have when you used it, I don't know how long ago that was :-)
paxdiablo
@paxdiablo: excuse me ? i didn't understand, i said that windows don't use backslash in path like unix. when did i said that python doesn't handle forward slash ?
singularity
You said not to use `f = open('E:/stuff/log.txt')` when in fact that will work fine. You don't need backslashes.
paxdiablo
@paxdiablo:correct me if i'm wrong, but i think that windows don't have path like this __E:/stuff/log.txt__ no forward slash, they use backslash like this : __E:\stuff\log.txt__ , you see the difference :)
singularity
Yes I see the difference but Windows itself handles this as per http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#paths : "Note File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\\?\" prefix as detailed in the following sections." You can see this in the question. The code used forward slash but the error message used backslash.
paxdiablo
@paxdiablo : ahh ok, thank you very much for the info, like i said it's been a long time that i didn't use windows :), and so windows normalize the path without the need to call : os.path.normpath() , heh !!
singularity
+2  A: 

Firstly, from above, Windows supports / just fine.

Secondly: Well, if you look at your file, you'll notice it's not log.txt, it's log.txt.txt... You may see it as "log.txt" in your graphical folder viewer (as opposed to the CLI "dir" command) simply because it hides the known file extensions.

I recommend you disable this - see folder options, there should be an option "Hide extensions of known file types" (or similar).

Tim Čas
+1  A: 

Define you path names using os.path.join()

root="E:\\"
mylog = os.path.join(root,"stuff","log.txt") # or log.txt.txt as seen in your dir output
f = open(mylog)
...
f.close()
ghostdog74
+3  A: 

Look at this line in the "dir" output:

23. 10. 2010  15:47                 0 log.txt.txt

The file you are looking for is named "log.txt.txt", not "log.txt". I see this happen when people set up the Windows file manager to not show known file extensions and then they try to add or modify an extension. I recommend to others that they turn this behavior off. You can do this under View->Folder Options I believe.

brady