简体   繁体   中英

Missing a file with os.walk()

I'm trying to make a file listing tool for a colleague. The code is quite simple:

source = C:\Users\Documents\test
extension = '.txt'
file_list = []
lower_levels = False


for root, dirs, files in os.walk(source):
    for n in files:
        if n.endswith(extension):
            file_list.append(n)
    if (not lower_levels):          #does not check lower levels
        break

writing_in_excel(source, file_list) #output is an excel file

When testing it on my test folder, it works pretty fine, I get all my 121 files listed in the output.

However, when my colleague tries it, one file is missing compared to the number of files given by Windows (I verified, windows indicates 39735 files wih the right extension, for 39734 in the excel file) and given the number of files, it's hard to find out which file is missing.

The problem doesn't seem to come from the writing in excel, since I write the total number of files with len(file_list) , and can already see that the file is missing in the list. I guess it comes from the walking in the directory??

Does anyone know where the problem could come from?

Thanks

There's probably an error that os.walk does not show by default. It can be handeled by setting the onerror parameter. Write an error handler:

def walk_error(error):
    print(error.filename)

Then change your call to:

for root, dirs, files in os.walk(source, onerror=walk_error):

Looks like the problem came from the extension condition, one of the file had the extension in caps. so I just replaced

if n.endswith(extension):

By

ext = os.path.splitext(n)[-1].lower()       #get the current file extension in lower case
if ext== extension:

And it works !

Thank you for your help.

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