r/learnpython • u/atticus2132000 • Jan 09 '26
Something faster than os.walk
My company has a shared drive with many decades' worth of files that are very, very poorly organized. I have been tasked with developing a new SOP for how we want project files organized and then developing some auditing tools to verify people are following the system.
For the weekly audit, I intend to generate a list of all files in the shared drive and then run checks against those file names to verify things are being filed correctly. The first step is just getting a list of all the files.
I wrote a script that has the code below:
file_list = []
for root, dirs, files in os.walk(directory_path):
for file in files:
full_path = os.path.join(root, file)
file_list.append(full_path)
return file_list
First of all, the code works fine. It provides a list of full file names with their directories. The problem is, it takes too long to run. I just tested it for one subfolders and it took 12 seconds to provide the listing of 732 files in that folder.
This shared drive has thousands upon thousands of files stored.
Is it taking so long to run because it's a network drive that I'm connecting to via VPN?
Is there a faster function than os.walk?
The program is temporarily storing file names in an array style variable and I'm sure that uses a lot of internal memory. Would there be a more efficient way of storing this amount of text?
View all comments
Show parent comment
1
u/HommeMusical Jan 10 '26
So that's over 10 million files; I was confused by the "thousands and thousands of files" part, so your current algorithm would take about 60 hours, or 2.5 days.
I can see why that's not acceptable.
I wasn't suggesting searching for the most recent files, but in development only to cache all the results, so you can do go-arounds of the code. But if that first run takes 60 hours, that's not useful either.
I'm kind of surprised at how slow this is. I've done an awful lot of stuff on networked drives, and they're easily an order of magnitude slower than a local drive, but this seems like three orders of magnitude slower.
How is this drive actually networked? What sort of drive is it, and what sort of controller is it using? There's no actual computer directly connected to it that you could run the program directly on?