-2

I'm learning through trying to make things and today I'm trying to get a program to give in bytes the total size of a directory, then display it's size. So from what I have read online, I kind of understand what is going on up until the point of printing the data. Obviously I know it's wrong as it's not working.

Any help would be really appreciated, thank you !

#!/usr/bin/env python
import os 

folder = raw_input("Folder Path : ")

folder_size = 0
for (path, dirs, files) in os.walk(folder):
  for file in files:
    filename = os.path.join(path, file)
    folder_size += os.path.getsize(filename)

print ("Folder Size = ") + (folder_size) 
  • Possible duplicate of [Calculating a directory size using Python?](https://stackoverflow.com/questions/1392413/calculating-a-directory-size-using-python) – Rahul Aug 01 '17 at 11:30
  • @Rahul I don't think it's a dupe, he's asking about the print statement, not about the logic of folder-size (perhaps it's a dupe of another thing regarding print statements, but not of this) – Ofer Sadan Aug 01 '17 at 11:45
  • @OferSadan: Ok. – Rahul Aug 01 '17 at 11:49

1 Answers1

3

The error is located in your print function.

Use format or + to concatenate strings and numbers. For instance, you can do:

#!/usr/bin/env python
import os

folder = raw_input("Folder Path : ")

folder_size = 0
for (path, dirs, files) in os.walk(folder):
  for file in files:
    filename = os.path.join(path, file)
    folder_size += os.path.getsize(filename)

print ("Folder Size = {}".format(folder_size))
Antoine
  • 1,393
  • 4
  • 20
  • 26