【问题标题】:python search path for data files (non-modules) - text files (*txt)数据文件(非模块)的python搜索路径 - 文本文件(*txt)
【发布时间】:2013-06-28 19:07:11
【问题描述】:

基于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 语句中搜索 modules 的路径。

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

是 sys.path 本身还是?

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

【问题讨论】:

标签: python


【解决方案1】:

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

【讨论】:

  • 天哪,我不得不读了几遍才能理解他在问什么......干得好(+1)
【解决方案2】:

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

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

还有我们的脚本:

#script.py

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

#act on file

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多