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 e开发者_如何学Gonter 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.
I assume you're using walk because you want to know every single file in the directory and its subdirectories. I do not understand what happens here:
file_count = sum((len(f) for _, _, f in os.walk(path)))
Assuming path contains, let's say, 'src' which is a directory in my home dir I get the number of files in the dir and its descendants, so what do you mean? Are you sure you're reading correctly the path from the command line? Can you post more?
sum
should return an integer, as it does in my python shell, too ...
>>> x = '/tmp'
>>> file_count = sum((len(f) for _, _, f in os.walk(x)))
>>> file_count
11
>>> type(file_count)
<type 'int'>
Since I had only files in that directory, i used this instead:
file_count = len([f for f in os.listdir(loadpathTest) if os.path.isfile(os.path.join(loadpathTest, f))])
This seems to work.
@All Thanks for your help.
精彩评论