Hello, I needed to find the number of files in a folder on the system.
This is what i used:
file_count = sum((len(f) for _, _, f in os.walk('path')))
This works fine when we specify the path as a string in quotes, but when I enter a variable name that holds the path, type(file_count) is a generator object, and hence cannot be used as an integer.
How to solve this and why does this happen?
Ok, here's what i'm doing:
in the command line at the terminal:
python mypyProg.py arg1 arg2 arg3
in myProg.py:
arg1 = sys.argv[1]
file_count = sum((len(f) for _, _, f in os.walk(arg1)))
arg1 is passed as a string
I checked repr(arg1) and type(arg1):
repr(arg1) '/home/kartik/Downloads/yahoo_dataset/tryfolder'
type(arg1) <type 'str'>
type(file_count) <type 'generator'>
Error message:
NDCG = scipy.zeros((file_count,1),float)
TypeError: an integer is required
I don't know, it is running fine in the IDLE python IDE when i enter it using just some dummy variables.