I’m making a bit of a stupid program that’s supposed to look like a basic operating system in python, except it’s just some fancy fake words while it “boots up”, then some basic functionlities. I made it print “booting up…” for a random amount of time, and to make it look cool I want it to go through every folder on the computer, and read every file in them (using os obviously). I can’t figure out how to though.
So I’ve used os.listdir() and a path, works fine. Now I want to use a loop to go inside every folder on my laptop, print the name of every file and if it sees a other folder it goes inside and does the same. Any suggestions on how to do this?
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
import os
def list_every_file(directory :str) ->None:
files = os.listdir(directory)
for file in files:
actual_path= directory + "\" + file
if os.path.isdir(actual_path):
list_every_file(actual_path)
else:
#print the content of the file
By using Something like that you’ll be able to list all the file starting from a directory, you can add some verification like the authorization to read or checking if the directory exist at first
Kaiwinta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.