简体   繁体   中英

Ignoring windows hidden files with python glob

I am moving some files with a python script. The script should work on both osx and windows.

I am using the the glob module to select the files. Filter out directories with isfile method from os.path. The glob module automatically ignores unix . files but it seems that it does grab some windows hidden files. I have added code to remove one "desktop.ini" that seems to have appeared in windows.

Are there any other Windows files that might appear or is there a way to ensure that I do not select hidden files in Windows?

files = glob.glob('*')
files = filter(os.path.isfile, files)  # filter out dirs
if "desktop.ini" in files : files.remove('desktop.ini')
# then using "shutil.move" to actually move the files

You might want to try Formic .

from formic import FileSet
fileset = FileSet(directory="/some/where/interesting",
              include="*.py",
              exclude=["desktop.ini", ".*", "addition", "globs", "here"]
              )
for filename in fileset:
    # use shutil to move them

This is a Python library using Globs, but i) already understands most hidden files (list of builtins here ), and ii) allows you to specify any files to exclude from the results ( documentation )

Disclosure: I am the maintainer.

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