Mostly Linux & Python syntax notes and hyperlinks.

Friday, March 20, 2015

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


No comments:

Post a Comment