2019-05-13 00:17:55

So, for the life of me, I can't figure out what does the os.walk(directory) do. I know it scans the files in some way, but I just don't understand... how?
I saw a loop that had 3 arguments when using os.walk. Something like this.
for root, drs, files in os.walk("documents")
What is the root? What is drs? What are files? How does it all connect to windows file system, what do I use if I want to get information about a file... what about storing a path to something I found?
I'm sure I'm being dumb once again, but can someone help out here?

2019-05-13 00:28:19 (edited by Ethin 2019-05-13 00:28:40)

Root is the directory your walking. drs is the directories within that folder. Files are the actual files in that folder. It is possible using the drs return value to create a recursive directory scanner. As a side note, I'd recommend using os.scandir() instead of os.walk.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-05-13 01:07:21 (edited by amerikranian 2019-05-13 01:07:51)

Thanks for the quick reply. I looked at os.scandir and it only seems to loop through the folders and work on the root of the folder. So if I have my documents folder and I use os.scandir on it, if I have any folders within documents it won't show the files inside it.

2019-05-13 01:16:03

No, it won't. Os.walk doesn't do that either.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-05-13 06:33:07 (edited by Sam_Tupy 2019-05-13 06:35:19)

@Ethin os.walk is indeed recursive. @1, basically when you loop through output from os.walk, you are getting a list with 3 items in it. This is usually represented by something like for root, dirs, files in os.walk(directory). However you can also execute for i in os.walk(directory) to get the above mensioned list. The first item, or root, is the root of the directory currently being walked. The second element or dirs, is a list of directories in the folder being walked, and the 3rd element, or files, is a list of files in the current directory being walked. So for example if you walk through documents and you have a folder called music within your documents folder, as well as a file called school_report.txt, and in the music folder you have a file called track1.mp3, the first iteration of the loop will give you the list ["documents", ["music"], ["school_report.txt"]]. Then music being the only directory, the next iteration will give you the list ["documents\\music", [], ["track1.mp3"]]. So yes os.wwalk will automatically scan subfolders, just to clear up that confusion. If you wish to scan a directory non recursively, you can use the os.listdir function, which simply returns a list of the files and the directories of the folder you give it. To see whether something is a directory or a file, you will have to call os.path.isdir and os.path.isfile on each item indivisually. Usually os.walk is the way to go!

I am a web designer, and a game developer. If you wish see me at http://www.samtupy.com

2019-05-13 19:58:09 (edited by Ethin 2019-05-13 20:00:38)

@5, that's odd -- it always seemed like os.walk was non-recursive to me. Maybe it was the examples I was looking at.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github