0

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
asked Jul 24, 2012 at 4:06

3 Answers 3

1

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 :)

answered Jul 24, 2012 at 4:22
Sign up to request clarification or add additional context in comments.

3 Comments

I tried path = raw_input( "Insert path: ") ,its working fine
But the other one flag an error NameError: name 'list _of_line' is not defined
A small mistake list_of_line should be replaced with list_of_lines :-) in above code 's' is missing
0

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

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.