-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) 
3
  • Possible duplicate of Calculating a directory size using Python?
    – Rahul
    Commented Aug 1, 2017 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
    Commented Aug 1, 2017 at 11:45
  • @OferSadan: Ok.
    – Rahul
    Commented Aug 1, 2017 at 11:49

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.