Lyra Orwell :
I am trying to count the number of files in a directory. This program counts the number of photos of a person. The person's name is used as the file's name.
Here is the full file path
C:\Users\barry\PycharmProjects\face_rec\images\Barry
I have looked at How to count the number of files in a directory using Python and came up with this solution:
numberOfFile=(len([filename for filename in os.listdir('images/'+name.get()) if os.path.isfile('images/'+name.get())]))
print(numberOfFile)
However this solution always returns 0
I would like to avoid using the absolute path, but if the only option is using the absolute path, that is fine.
blhsing :
You aren't making use of the filename
variable with which you iterate through the list returned by os.listdir
. You can use os.path.join
to join the directory name with it so that it can be found by os.path.isfile
:
numberOfFile = len([filename for filename in os.listdir('images/' + name.get())
if os.path.isfile(os.path.join('images/' + name.get(), filename))])
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=405474&siteId=1