1

Base on http://docs.python.org/release/1.5.1p1/tut/searchPath.html

Search Path (sys.path)

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

Its a path to search for modules in import statements.

I am wondering which path is used for search for data files (*txt files etc). For eg. If I do a fs.open(someFile), which all paths python will search for it?

Is it sys.path itself or ?

My confusion is that the docs say sys.path is a search path for modules and data files are not modules.

1

2 Answers 2

3

There is no such option. Only the current working directory is searched if no path is specified in the filename.

Sign up to request clarification or add additional context in comments.

1 Comment

holy cow I had to read that a few times to understand what he was asking... nice work(+1)
1

As Ignacio says, there is no built in variable for this. Either the user or the script itself must supply the directory or directories to search for files.

python script.py /path/to/file/file_to_parse

And our script:

#script.py

import sys
my_file = open(sys.argv[1], 'r')

#act on file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.