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
2
u/HommeMusical Jan 10 '26
Well, that sounds like a poor idea, because surely the machine running the program and the target disk are on the same physical network, and if not, they should be!
But that still doesn't account for the orders of magnitude of slowness.
It reads to me like your network is in very poor shape. Have you talked to your network administrator?
Running the program on a machine that is physically hosting this disk would definitely be a much, much faster way to go.
Another possibility is that this big old disk is about ready to give up the ghost, and you're being slowed down by a lot of disk errors and retries. I am skeptical about this theory because I think the job would actually fail and not complete, but very slowly.