Wednesday, 15 August 2012

strncmp in python -



strncmp in python -

i parsing through file list of paths. trying see if 1 path under specific directory or not. have 2 strings s1 , s2. lets s1 = '/tmp/' , s2 = '/tmp/file.txt'

if want check if s2 has s1 , bytes in c, strncmp of s1 , s2 upto strlen(s1) bytes. there way in python? new python , not know modules available me yet. implement trivially iterating on characters in strings , comparing, want find out if there gives me these kind of helper functions default

thanks help.

p

yes. can do: if in b: check if a substring anywhere in b.

e.g.

if 'foo' in 'foobar': print true if 'foo' in 'barfoo': print true

from post, appears want @ start of strings. in case, can utilize .startswith method:

if 'foobar'.startswith('foo'): print "it does!"

similarly, can same thing endswith:

if 'foobar'.endswith('bar'): print "yes sir :)"

finally, maybe literal translation of strncmp utilize slicing , ==:

if a[:n] == b[:n]: print 'strncmp success!'

python has many facilities dealing path names in os.path module. it's worth investigating in there. there pretty neat functions.

python

No comments:

Post a Comment