简体   繁体   中英

Exclude temporary files using glob

Is there a way to exclude temporary files without writing more lines of code?

for file in glob.glob("C\user1\*.xlsx"):
    print(file)

C\user1\apple.xlsx
C\user1\~$apple.xlsx

I tried:

for file in glob.glob("C\user1\[^\w]*.xlsx"):
    print(file)

You can't use regular expression escape sequences in globs. You also have it backwards -- you want the first character to be a word character, not exclude them.

In filename wildcards, use [!characters] to exclude characters, not [^characters] . See the fnmatch module.

for file in glob.glob(r"C\user1\[!~]*.xlsx"):
    print(file)

You could do something like this:

for file in filter(lambda x: x[0] != '~', glob.glob('C\user1\*.xlsx')):
    print(file)

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