How to list files in a folder with size

Asked

Viewed 463 times

-2

Hello, I need to list all the files in a folder with several subfolders, I need the name and size of all the files in this folder, I’m trying to make a code in Python for this but I’m not getting, can someone please help?

I started work this week and I’m still learning to use python, so I don’t have the code ready.

  • 1

    Could [Edit] the question add the code you have done so far and describe what was the result obtained? It would also be interesting to add an example folder structure and put what would be the expected result for this example.

  • Jean, be very careful not to distort your question with the edits. By adding new requests you disable the current answers. If your intention was to generate a spreadsheet with the values, this should be described from the beginning. You’ve seen what it’s like to list the files and their respective sizes, so I think you already have enough material to try to generate the spreadsheet yourself. If you can’t, open a new question.

3 answers

1

As of Python version 3.4, you have no reason to use the package os. A new package has been added, pathlib, to deal with directories and files that abstract much in a more organized way the functions to manage a directory.

If we consider a folder structure:

pasta/
    subpasta/
        subpasta/
            arquivo_4.txt
        arquivo_3.txt

    arquivo_1.txt
    arquivo_2.txt

We can do:

from pathlib import Path

pasta = Path('pasta')

for f in pasta.glob('**/*'):
    if f.is_file():
        print(f.name, f.stat().st_size)

See working on Repl.it

The output would be similar to:

arquivo_2.txt 0
arquivo_1.txt 0
arquivo_3.txt 0
arquivo_4.txt 0
  • Gave this error: runfile(’D:/Users/Diogo.silva/.Spyder-py3/temp.py', wdir=’D:/Users/Diogo.silva/.Spyder-py3') File "D:/Users/Diogo.silva/. Spyder-py3/temp.py", line 3 folder = Path(’D: Users Diogo.silva test') Syntaxerror: (Unicode error) 'unicodeescape' codec can’t Decode bytes in position 2-3: truncated UXXXXXXXX escape

  • @Jeanribeiro This is another error unrelated to the question: My String is giving error "unicodeescape"

  • Valeeeeu, I got it, but since there are more than 9k of files, I can play this data directly in an excel spreadsheet through python?

  • @Jeanribeiro has how, yes. Instead of the print just write in the file the data you want.

  • Thanks, thank you very much, is there any way to print this list in Kbytes? when I am printing the list comes in bytes

  • @Jeanribeiro How many bytes is 1kbyte?

Show 1 more comment

0

You can generate a dictionary of all files and sizes with the following code:

import os

path = "C:\\seu\\path\\"
lista = [f for f in os.listdir(path)]
tamanhos = []
dict_arquivos = {}

for item in lista:
    dict_arquivos[item] = os.path.getsize(path+item)

print(dict_arquivos)
  • Which version of Python you are using?

  • 1

    I forgot to put the ones in front. I got the code, Wallace.

  • Gave this error: runfile(’D:/Users/Diogo.silva/.Spyder-py3/temp.py', wdir=’D:/Users/Diogo.silva/.Spyder-py3') File "D:/Users/Diogo.silva/. Spyder-py3/temp.py", line 3 path = "D: Users Diogo.silva test" Syntaxerror: (error Unicode) 'unicodeescape' codec can’t Decode bytes in position 2-3: truncated UXXXXXXXX escape

0

In a very small way, you can write your code like this:

from os import path, listdir

pasta = '/diretorio'

lista = [dict(arquivo=f, tamanho=path.getsize(path.join(pasta, f))) for f in listdir(pasta)]

The result will be something like:

[
    {'arquivo': 'nota fiscal 08 04 2019.pdf', 'tamanho': 11074}, 
    {'arquivo': 'nota fiscal 08 05 2019.pdf', 'tamanho': 11069}, 
    {'arquivo': 'install.sh', 'tamanho': 1374}, 
    {'arquivo': 'NOTA FISCAL.pdf', 'tamanho': 11028}, 
    {'arquivo': 'test.pdf', 'tamanho': 176427}
]

In the example above, we use a comprehensilist on to generate a list with a dict, containing the values arquivo esize`.

The function path.join is used with the variable pasta because the listdir does not return the full file path, but only its base name inside the folder.

In the tests I did, I used Python version 3.6 and 2.7.

Recursion

If you need to list both directories and subdirectories, I suggest using recursion. I did it this way:

from os import path, walk

pasta = '/home/wallace/Documentos'

def recursive_walk(pasta):

    lista = []

    for raiz, subdirs, arquivos in walk(pasta):
        for f in arquivos:
            arquivo = path.join(raiz, f)
            lista.append(dict(arquivo=arquivo, tamanho=path.getsize(arquivo)))

        for subdir in subdirs:
            lista += recursive_walk(subdir)


    return lista

lista = recursive_walk(pasta)

print(lista)
  • Hello, I managed to run the program, but there are more than 9k of files, there is some way to play these files in excel spreadsheet?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.