数据文件(非模块)-文本文件(*txt)的python搜索路径



基于http://docs.python.org/release/1.5.1p1/tut/searchPath.html

搜索路径(系统路径)

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

它是在导入语句中搜索模块的路径。

我想知道哪个路径用于搜索数据文件(*txt文件等)。例如,如果我做一个fs.open(someFile),python会搜索哪个路径?

是sys.path本身还是?

我的困惑是,文档说sys.path是模块的搜索路径,而数据文件是not模块。

没有这样的选项。如果文件名中未指定路径,则只搜索当前工作目录。

正如Ignacio所说,没有内置的变量。用户或脚本本身必须提供一个或多个目录来搜索文件。

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

我们的脚本:

#script.py
import sys
my_file = open(sys.argv[1], 'r')
#act on file

最新更新