1

I know there are questions like that but I still wanted to ask this because I couldn't solve, so there is my code:

#! python3
import os

my_path = 'E:\\Movies'
for folder in os.listdir(my_path):
    size = os.path.getsize(folder)
    print(f'size of the {folder} is: {size} ')   

And I got this error:

Traceback (most recent call last):
File "c:/Users/ataba/OneDrive/Masaüstü/Programming/python/findingfiles.py", line 7, in <module>
size = os.path.getsize(folder)
File "C:\Program Files\Python37\lib\genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'FordvFerrari1080p'

When I write print(folder) instead of getting their size it shows the folders so I don't think the program can't find them.

martineau
  • 119,623
  • 25
  • 170
  • 301
altF4
  • 269
  • 4
  • 19
  • That is probably because you are trying to get the size of a folder, which `os.path.getsize()` can't do; it only works with files and crashes because you are passing folders intead of files. You need to iterare over all the files contained in the folder and sum their sizes. See [this](https://stackoverflow.com/questions/1392413/calculating-a-directorys-size-using-python) – TitoOrt Feb 25 '20 at 14:00
  • The system cannot find `FordvFerrari1080p`, because there is no such file *in the current working directory* - it's actually in `E:\Movies` instead. You need to use `os.path.join(my_path, folder)` to get a full pathname to where the file is actually located. – jasonharper Feb 25 '20 at 14:13
  • Also note that `os.listdir()` returns the names of _files_ in the specified directory, not the folders. – martineau Feb 25 '20 at 14:39
  • @TitoOrt yeah, I looked at it one more time and finally got it, thank you. – altF4 Feb 25 '20 at 19:39

2 Answers2

0

The problem may be that you are passing as an argument to os.path.getsize() just the folder name instead of the whole path to the folder

FabZanna
  • 181
  • 1
  • 10
0

It may be that you have the file name as 'FordvFerrari1080p' rather than 'FordvFerrari1080p.mp4' (or whatever file type it may be)

Alfie Atkinson
  • 58
  • 1
  • 10