I am trying to make a small program that looks through a directory (as I want to find recursively all the files in the sub directories I use os.walk()).
Here is my code:
import os
import os.path
filesList=[]
path = "C:\\Users\Robin\Documents"
for(root,dirs,files) in os.walk(path):
for file in files:
filesList+=file
Then I try to use the os.path.getsize() method to elements of filesList, but it doesn't work.
Indeed, I realize that the this code fills the list filesList with characters. I don't know what to do, I have tried several other things, such as :
for(root,dirs,files) in os.walk(path):
filesList+=[file for file in os.listdir(root) if os.path.isfile(file)]
This does give me files, but only one, which isn't even visible when looking in the directory.
Can someone explain me how to obtain files with which we can work (that is to say, get their size, hash them, or modify them...) on with os.walk ? I am new to Python, and I don't really understand how to use os.walk().
The issue I suspect you're running into is that
filecontains only the filename itself, not any directories you have to navigate through from your starting folder. You should useos.path.jointo combine the file name with the folder it is in, which is therootvalue yielded byos.walk:Now all the filenames in
filesListwill be acceptable toos.path.getsizeand other functions (likeopen).I also fixed a secondary issue, which is that your use of
+=to extend a list wouldn't work the way you intended. You'd need to wrap the new file path in a list for that to work. Usingappendis more appropriate for adding a single value to the end of a list.