简体   繁体   中英

Looping through a directory of files in Python

I'm 99% of the way through my first python script, but I'm getting tripped up on the equivalent of a for-each loop through files in a directory. My script is working for single files, I'm just not sure how to apply it to multiple files, one at a time.

I have a path path = ~/documents and an XML file with filenames I would like to exclude:

 <root><synced name="Already Synced"><sfile name="Filename">base</sfile><sfile name="Filename">File1.blah</sfile><sfile name="Filename">File2.blah</sfile><sfile name="Filename">File3.blah</sfile></synced></root>

how would I run my script on, say, all files that end with *.blah and are NOT in the XML sfile?

I had this, but it was a no-go:

path = '~/documents'
tree = ET.parse("sync_list.xml")
root = tree.getroot()
for elem in root.findall('sfile'):
    synced = elem.text
do_library = os.listdir(path)
if glob.fnmatch.fnmatch(file,"*.blah") and not synced:
  for entry in do_library:
    file = os.path.join(path, entry)
    result = plistlib.readPlist('file')

Thank you so much for any help you can offer.

import fnmatch
import os

path = os.path.expanduser('~/documents')
tree = ET.parse("sync_list.xml")
root = tree.getroot()
synced = [elt.text for elt in root.findall('synced/sfile')]
for filename in os.listdir(path):
    if fnmatch.fnmatch(filename, '*.blah') and filename not in synced:
        filename = os.path.join(path, filename)

Edit: Added os.path.expanduser, as suggested by @mata.

path = '~/documents'

this doesn't give you the documents folder in your home-directory. ~ doesn't really stand for the home directory, what lets's you use it as if it were usually is the shell doing tilde expansion. Without that, ~ can be the name of any file or directory, and python treats it as such. To get it correctly, use:

path = os.path.expanduser('~/documents')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM