0

以下のコードでは、ハードコードされたパスを置き換える方法は?設定ファイルからパスを読み取る方法はありますか?

import sys  
sys.path.append(r" ./build/lib.linux-x86_64-2.4")
4

3 に答える 3

1

シェルからパスを読み取ることができます:

path = raw_input( "Insert path: ") # It will display "Insert path and will return the string entered into variable 'path'

またはファイルを使用して:

f = fopen( "<filepath>", "r" ) #open the file in reading-mode
list_of_lines = f.readlines() # read all the lines and put them in a list
f.close() # closes the file
for i in range( len(list_of_lines ) ): #cleaning from newline characters
  list_of_line[i] = list_of_line[i].strip()

そして今、list_of_lines リストを int すると、ファイルから読み取られたすべての行が表示されます...たとえば、次のことができます。

for i in range(len(list_of_lines)):
  sys.path.append( list_of_lines[i] )

それが役立つことを願っています:)

于 2012-07-24T04:22:16.473 に答える
0

置き換える代わりに、sys.path.insert(0, "./build/lib.linux-x86_64-2.4")そのパスを優先することができます。

于 2012-07-24T04:10:36.543 に答える
0

Python has something called .pth files for this purpose.

于 2012-07-24T04:12:48.973 に答える