os.walk in python, probleme due to folder labeled '_' - Stack Overflow

admin2025-04-17  3

On a windows system using Python 3.13.1, I'm trying to get a list of all the files and folders in my directory, and I have a script using os.walk that goes through the entire directory and gets the file and folder names

However, when the program gets to the folder labeled _, the program stops turning and is blocked (I tested this by using print statements in the both for loops). The program turns in my parent folder, displaying the files inside, but when it goes down a layer, where the folder _ is, the program stops displaying anything. Any help would be appreciated.

Parent folder contains 17000 child folders, and those child folders have names containing accents (French), periods, apostrophes, commas, and hyphens.

import os
import re
import datetime

mypath = input('Chemin source : ')
f,d,t,e = [],[],[],[]
for (dirpath, dirnames, filenames) in os.walk(mypath):
    print(dirpath)
    for i in filenames: 
        print(i)
        d.extend([r"{}".format(dirpath)])
        f.extend([i])
        t.extend([datetime.date.fromtimestamp(os.path.getmtime(r"{}".format(dirpath)+'\\'+ i))])
        e.extend(['.'+i.split('.')[-1]])
print('done')

I expected it to go through the entire directory, which it did normally on a different directory, but not when there is a _ as the entire folder name

转载请注明原文地址:http://anycun.com/QandA/1744903239a89252.html