在下面的代码中如何替换硬编码路径? 有没有什么办法从一些配置文件读取路径?如何替换python中的硬编码路径
import sys
sys.path.append(r" ./build/lib.linux-x86_64-2.4")
在下面的代码中如何替换硬编码路径? 有没有什么办法从一些配置文件读取路径?如何替换python中的硬编码路径
import sys
sys.path.append(r" ./build/lib.linux-x86_64-2.4")
而不是取代你可以做sys.path.insert(0, "./build/lib.linux-x86_64-2.4")
这将优先考虑该路径。
为此,Python有一些名为.pth
的文件。
你可以阅读从壳路径:
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列出你拥有所有的线从文件中读取。 ..For例如,现在您可以:
for i in range(len(list_of_lines)):
sys.path.append(list_of_lines[i])
希望它能帮助:)
我试过path = raw_input(“Insert path:”),其工作正常 – jaysh 2012-07-24 05:13:28
但另一个标记错误NameError:name'list _of_line'未定义 – jaysh 2012-07-24 05:18:49
一个小小的错误list_of_line应该用list_of_lines :-)替换上面的代码's'缺失 – jaysh 2012-07-24 05:31:31