Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the number of files in a folder using Python?

Tags:

python

file-io

How do I read the number of files in a specific folder using Python? Example code would be awesome!

like image 578
Noah R Avatar asked Oct 07 '10 15:10

Noah R


People also ask

How do I see the number of files in a folder?

Browse to the folder containing the files you want to count. Highlight one of the files in that folder and press the keyboard shortcut Ctrl + A to highlight all files and folders in that folder. In the Explorer status bar, you'll see how many files and folders are highlighted, as shown in the picture below.

How do you count the number of files in a file in Python?

Use readlines() to get Line Count This is the most straightforward way to count the number of lines in a text file in Python. The readlines() method reads all lines from a file and stores it in a list. Next, use the len() function to find the length of the list which is nothing but total lines present in a file.

How do I read multiple files in a directory in Python?

Import the OS module in your notebook. Define a path where the text files are located in your system. Create a list of files and iterate over to find if they all are having the correct extension or not. Read the files using the defined function in the module.


2 Answers

To count files and directories non-recursively you can use os.listdir and take its length.

To count files and directories recursively you can use os.walk to iterate over the files and subdirectories in the directory.

If you only want to count files not directories you can use os.listdir and os.path.file to check if each entry is a file:

import os.path
path = '.'
num_files = len([f for f in os.listdir(path)
                if os.path.isfile(os.path.join(path, f))])

Or alternatively using a generator:

num_files = sum(os.path.isfile(os.path.join(path, f)) for f in os.listdir(path))

Or you can use os.walk as follows:

len(os.walk(path).next()[2])

I found some of these ideas from this thread.

like image 60
Mark Byers Avatar answered Sep 17 '22 17:09

Mark Byers


pathlib, that is new in v. 3.4, makes like easier. The line labelled 1 makes a non-recursive list of the current folder, the one labelled 2 the recursive list.

from pathlib import Path

import os
os.chdir('c:/utilities')

print (len(list(Path('.').glob('*')))) ## 1
print (len(list(Path('.').glob('**/*')))) ## 2

There are more goodies too. With these additional lines you can see both the absolute and relative file names for those items that are files.

for item in Path('.').glob('*'):
    if item.is_file():
        print (str(item), str(item.absolute()))

Result:

boxee.py c:\utilities\boxee.py
boxee_user_catalog.sqlite c:\utilities\boxee_user_catalog.sqlite
find RSS.py c:\utilities\find RSS.py
MyVideos34.sqlite c:\utilities\MyVideos34.sqlite
newsletter-1 c:\utilities\newsletter-1
notes.txt c:\utilities\notes.txt
README c:\utilities\README
saveHighlighted.ahk c:\utilities\saveHighlighted.ahk
saveHighlighted.ahk.bak c:\utilities\saveHighlighted.ahk.bak
temp.htm c:\utilities\temp.htm
to_csv.py c:\utilities\to_csv.py
like image 22
Bill Bell Avatar answered Sep 20 '22 17:09

Bill Bell