簡體   English   中英

數據文件的python搜索路徑(非模塊) - 文本文件(* txt)

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

基於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模塊。

沒有這樣的選擇。 如果文件名中未指定路徑,則僅搜索當前工作目錄。

正如Ignacio所說,沒有內置變量。 用戶或腳本本身必須提供一個或多個目錄來搜索文件。

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

我們的腳本:

#script.py

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

#act on file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM