Eye of Hell Eye of Hell - 19 days ago 5
Python Question

How to remove the left part of a string?

I have some simple python code that searches files for a string e.g.

path=c:\path
, whereby the
c:\path
may vary. The current code is:

def findPath( i_file) :
lines = open( i_file ).readlines()
for line in lines :
if line.startswith( "Path=" ) :
return # what to do here in order to get line content after "Path=" ?


What is a simple way to get a string text after
Path=
?
Is there a simple method, without closures, reflection or other esoteric things ?

Answer

If the string is fixed you can simply use:

if line.startswith("Path="):
    return line[5:]

which gives you everything from position 5 on in the string (a string is also a sequence so these sequence operators work here, too).

Or you can split the line at the first =:

if "=" in line:
    param, value = line.split("=",1)

Then param is "Path" and value is the rest after the first =.