Search code examples
pythonpython-3.xglob

Best way to slpit(',') indefinite string length and assign elements to variables


What would be the best way to assign each element into to a variable, if the length of 'myext' is unknown? I have four assigned variables, but what if I had a 5th ext in 'myext'?

def check_files(mydir, myext, name):

    extensions = myext.split(',')  
    check1 = mydir + extensions[0]
    check2 = mydir + extensions[1]
    check3 = mydir + extensions[2]
    check4 = mydir + extensions[3]
    Output1 = glob.glob(check1)
    Output2 = glob.glob(check2)
    Output3 = glob.glob(check3)
    Output4 = glob.glob(check4)



check_files('path', '*.pdf,*.xml,*.sff,*.idx', 'Test 1')

Solution

  • You can use some list comprehensions to iterate over the whole of extensions.

    checks = [mydir+extension for extension in extensions]
    outputs = [glob.glob(check) for check in checks]