以下のコードでは、ハードコードされたパスを置き換える方法は?設定ファイルからパスを読み取る方法はありますか?
import sys
sys.path.append(r" ./build/lib.linux-x86_64-2.4")
以下のコードでは、ハードコードされたパスを置き換える方法は?設定ファイルからパスを読み取る方法はありますか?
import sys
sys.path.append(r" ./build/lib.linux-x86_64-2.4")
シェルからパスを読み取ることができます:
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] )
それが役立つことを願っています:)
置き換える代わりに、sys.path.insert(0, "./build/lib.linux-x86_64-2.4")
そのパスを優先することができます。
Python has something called .pth
files for this purpose.