1

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

搜索路径 (sys.path)

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

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

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

它是 sys.path 本身还是?

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

4

2 回答 2

3

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

于 2013-06-28T19:08:47.123 回答
1

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

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

还有我们的脚本:

#script.py

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

#act on file
于 2013-06-28T19:11:46.137 回答