-3

I have to make a program that measures the size of a given directory by using recursivity. I need to use the OS module. When I used

os.path.getsize()

The response was 0 bytes, which makes no sense.

sam
  • 2,263
  • 22
  • 34
Chifrijo
  • 66
  • 7
  • 1
    You'll need to post code for us to help. Recursion..., are you interesting in sizes of the entire subtree? `os.walk` works without recursion. – tdelaney May 30 '20 at 03:16

2 Answers2

0

You need to mention the path in the argument of a getsize function. So it would look like:

os.path.getsize(<path_of_file>)

If you are doing that already and interested in the size of a folder, you can add size of the files in the folder iteratively.

sum(os.path.getsize(f) for f in os.listdir(<path_of_the_folder>) if os.path.isfile(f))
sam
  • 2,263
  • 22
  • 34
  • I now have this: def sdir_files_size(Ruta): archivos = os.listdir(Ruta) if os.path.isdir(Ruta): return get_size(archivos) else: return 'Error: la entrada no es un directorio' def get_size(archivos): if archivos == []: return [] else: return os.path.getsize(archivos[0]) + get_size(archivos[1:]) #But i am getting an error which says: FileNotFoundError: [WinError 2] The system cannot find the file specified: – Chifrijo May 30 '20 at 13:17
  • You need to provide a string as a file path. For example, os.path.getsize("Documents/") – sam May 30 '20 at 14:54
-1

I get your question, the same problem i also got, you can visit here for ans : https://github.com/ASHWIN990/sizeof/blob/master/sizeof

I have made a Pythton script which find the size of file and directories recurcively