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.

GJain
  • 5,025
  • 6
  • 48
  • 82
  • 1
    related: [Python : How to access file from different directory](http://stackoverflow.com/q/17244406/4279) – jfs Jun 28 '13 at 19:38

2 Answers2

3

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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
armstrhb
  • 4,054
  • 3
  • 20
  • 28