Python IDLE doesn't find text files unless I specify the path
Question: In my Python books and also the Python Documents this code should be enough to
open a file:

    
    
    f = open("file.txt", "r")
    

But if I do this I will get an error message telling me file.txt doesn't
exist. If I however use the whole path where file.txt is located it opens it:

    
    
     f = open("C:/Users/Me/Python/file.txt", "r")
    

Is there an explanation for this?


Answer: In short - the immediate search path (the current working directory) is where
Python will look... (so on Windows - possibly it'll assume C:\Pythonxy)

Yes, it depends on where Python/IDLE is executed... for it to use its search
path:

    
    
    >>> import os
    >>> os.getcwd()
    '/home/jon'
    
    >>> open('testing.txt')
    <open file 'testing.txt', mode 'r' at 0x7f86e140edb0>
    

And in a shell - changing directories... then launching Python/IDLE

    
    
    jon@forseti:~$ cd /srv
    jon@forseti:/srv$ idle
    
    >>> import os
    >>> os.getcwd()
    '/srv'
    >>> open('testing.txt')
    
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        open('testing.txt')
    IOError: [Errno 2] No such file or directory: 'testing.txt'
    

