Reputation: 3
I seem to be only able to find the "getsize" of my entire directory, instead of the individual files in the directory <= 64 bytes. Any ideas or suggestions would be really appreciated, thank you!
Here is my script:
#argument is directory path
import os, sys
myDir = sys.argv[1]
myFiles = os.listdir(myDir)
for eachOne in myFiles:
size = os.path.getsize(myFiles)
if size <= "64":
print "{0} is {1} bytes.".format(eachOne, size)
The goal is to print out the file name is XX bytes.
Upvotes: 0
Views: 359
Reputation:
There are three problems here:
You need to give os.path.getsize
eachOne
instead of myFiles
.
os.path.getsize
returns an integer. Meaning, you need to compare size
with another integer, not a string.
You need to use os.path.join
to give os.path.getsize
the full path to the file. Otherwise, Python will not know where the file is.
Below is a version of your script with these problems addressed:
import os, sys
myDir = sys.argv[1]
myFiles = os.listdir(myDir)
for eachOne in myFiles:
size = os.path.getsize(os.path.join(myDir, eachOne))
if size <= 64:
print "{0} is {1} bytes.".format(eachOne, size)
Upvotes: 1