dimanche 22 mars 2015

if item in list match item in list from child element in dictionary

brand new to python and stumped all ready, would appreciate a hand.



testn1 = {'names':('tn1_name1','tn1_name2','tn1_name3'),'exts':('.log','.txt')}
testn2 = {'names':('tn2_name1'),'exts':('.nfo')}
testnames = {1:testn1,2:testn1}

directory = 'C:\\temp\\root\\'

for subdir in os.listdir(directory):

# check if name of sub directory matches the name in any of the dicts in testnames[testn*]['names']
if os.path.isdir(os.path.join(directory, subdir)) and [subdir in subdir.lower() in testnames[testn1]['names']]: # this works but need to iterate through all dicts
print(subdir)

# if the a dir name matches do a recursive search for all filenames that exist in the same dict with the corresponding extensions
for dirname, dirnames, filenames in os.walk(os.path.join(directory, subdir)):
for file in filenames:
if file.endswith(testnames[testn1]['exts']): # this works but need to match with corresponding folder
print(file)


I thought i'd be able to do something like this but i'm sure my understanding of python isn't were it needs to be.



if os.path.isdir(os.path.join(directory, subdir)) and [subdir in subdir.lower() in [for testnames[key]['names'] in key, value in testnames.items()]]:


I'm hoping to keep it structured this way but would be open to anything.


Aucun commentaire:

Enregistrer un commentaire