In the below code how to replace the hardcoded path? Is there any way to read the path from some config file?
import sys
sys.path.append(r" ./build/lib.linux-x86_64-2.4")
javaPlease42
5,0017 gold badges39 silver badges67 bronze badges
3 Answers 3
You can read the path from shell:
path = raw_input( "Insert path: ") # It will display "Insert path and will return the string entered into variable 'path'
Or using a file:
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()
And now, int the list_of_lines list you'll have all the lines read from the files...For example, now you can:
for i in range(len(list_of_lines)):
sys.path.append( list_of_lines[i] )
Hope it help :)
Sign up to request clarification or add additional context in comments.
Instead of replacing you can do sys.path.insert(0, "./build/lib.linux-x86_64-2.4") which will give preference to that path.
answered Jul 24, 2012 at 4:10
Rohan
53.5k12 gold badges96 silver badges89 bronze badges
Comments
Python has something called .pth files for this purpose.
bluish
27.6k28 gold badges126 silver badges185 bronze badges
answered Jul 24, 2012 at 4:12
Ashwini Chaudhary
252k60 gold badges479 silver badges520 bronze badges
Comments
lang-py