Python's search path -- where it looks for modules named in import
statements -- includes the contents of the PYTHONPATH
environment variable, the value of sys.path
, and the directory in which the running script was located. Your current working directory (cwd
) is never part of Python's search path by default.
Your cwd
when you start your Python script is the cwd
of your script; this means that file operations, such as open()
, will refer to files in your current directory absent any path qualifiers. For example, if you are in c:\temp
, and you run a Python script, and you script does this...
fd = open('myfile.txt')
...then you will be opening c:\temp\myfile.txt
.
If you want to open files in another directory, you can provide a full path to open
:
fd = open('c:\\anotherdir\\myfile.txt')
Or you can call os.chdir()
in your code:
os.chdir('c:\\anotherdir')
fd = open('myfile.txt')