GJain
GJain

Reputation: 5093

python search path for data files (non-modules) - text files (*txt)

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.

Upvotes: 1

Views: 383

Answers (2)

armstrhb
armstrhb

Reputation: 4152

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

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

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

Upvotes: 3

Related Questions