Unable to use getsize method with os.walk() returned files

285 views Asked by At

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().

2

There are 2 answers

0
Blckknght On BEST ANSWER

The issue I suspect you're running into is that file contains only the filename itself, not any directories you have to navigate through from your starting folder. You should use os.path.join to combine the file name with the folder it is in, which is the root value yielded by os.walk:

for(root,dirs,files) in os.walk(path):
    for file in files:
        filesList.append(os.path.join(root, file))

Now all the filenames in filesList will be acceptable to os.path.getsize and other functions (like open).

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. Using append is more appropriate for adding a single value to the end of a list.

0
ohyeh On

If you want to get a list of files including path use:

for(root, dirs, files) in os.walk(path):
    fullpaths = [os.path.join(root, fil) for fil in files]
    filesList+=fullpaths