Reputation: 105
I know that this question may be silly, and I am OK with that since I am at a rookie level.
I would like to know whether there is a way to open a file in python by entering only its name , e.g open(mbox.txt)
instead of open(C:\Python27\mbox.txt)
??
Thanks
Upvotes: 0
Views: 184
Reputation: 1575
Python looks for file you mention, without their complete path, in the following directories listed in sys.path
import sys
for path in sys.path:
print path
you will get the list of directories in which the python looks for, if you would specify a file in a different directory than this list, just add the directory name into this and you can specify files in them without complete path.
Correction
The above suggestion does not answer the question, as Jon said it only affects import machinery. To answer your question, change the directory using os module of the python.
import os
os.chdir('to_the_respective_directory')
open('file.txt')
Upvotes: 1
Reputation: 98901
I would like to know whether there is a way to open a file in python by entering only its name
You can open a file without providing the path
, as long as the file is located on the same dir
as the python script.
Upvotes: 1