Mostly Linux & Python syntax notes and hyperlinks.

Friday, March 20, 2015

python: pathlist test with numbered output & avoiding empty values

Here's the code in the previous post with a few enhancements:

import os

def test_path(in_path,sep=';'):
    pl=in_path.split(sep)

    there=[]
    not_there=[]
    for t in pl:
        if len(t) > 1:
            if os.path.isfile(t):
                there.append(t)
            else:
                not_there.append(t)

    print "These exist"
    for i,t in enumerate(there):
        print "%d  %s" % (i+1,t)

    print "\nThese are missing:"

    for i, t in enumerate(not_there):
        print "%d  %s" % (i+1,t)


python: simple method to test pathlist

I'm converting a Window's batch script to run on my system. One of its lines defines a long semicolon-separated path list. I started to check them one-by-one, then realized there was a simpler way:

import os

def test_path(in_path,sep=';'):
    pl=in_path.split(sep)

    there=[]
    not_there=[]
    for t in pl:
        if os.path.isfile(t):
            there.append(t)
        else:
            not_there.append(t)

    print "These exist"
    for t in there:
        print t

    print "\nThese are missing:"
    for t in not_there:
        print t


Monday, March 16, 2015

python: return parent path containing input string

I wrote this to isolate higher-level directory paths from those listed in environment variables.

import os

def path_including_string(big_path,small_string): 
   """ Return parent directory of big_path whose name includes small_string
       Searches from end, so if small_string in two subdirectories, 
          then this returns the lowest (longest) path
   Keyword arguments:
   :param big_path: input path to be cut down
   :param small_string: string that defines where to end returned path
   """
   assert small_string in big_path
   head,tail=os.path.split(big_path)
   while small_string not in tail:
     big_path=head
     head,tail=os.path.split(big_path)
   return big_path


Simple Test:
>>> big_path="C:\\abc\\def\\ghi\\jkl\\cdef\\qr\\s\\tuv"
>>> path_including_string(big_path,"de") 
 'C:\\abc\\def\\ghi\\jkl\\cdef' 

Test using an environment variable:

>>> mylist=os.environ.get('LIB').split(';')
>>> for p in mylist:
...     if 'SDK' in p:
...         print p
...         print path_including_string(p,'SDK')
...        
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\\PlatformSDK\lib
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\\PlatformSDK
C:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib
C:\Program Files\Microsoft SDKs
 


python: return index of last character of a substring

find() and index() return index of first character of the input pattern within the searched string.
This returns the index of the ending character of the input pattern.

# Return index of last character of small_string
def end_index(big_string,small_string):
    assert small_string in big_string
    start_index=big_string.index(small_string)
    return start_index + len(small_string)