How to Print All Files in a Directory Using Python [5 Methods]

In this blog, you will learn how to print all files in a directory using Python. Python allows us to list all files inside a directory/folder in multiple ways. The ways to list all files in a directory are listed below:

  • listdir() method
  • walk() method
  • scandir() method
  • Using glob library
  • Methods from the pathlib library

Let’s look into each method in detail with an example program.

Also Read: How To Copy List in Python

Python Print All Files in a Directory

Method 1: Print all files in a directory using the listdir() method

The listdir() method returns the list of files and directories in the specified path. The listdir() method accepts an argument that represents the directory path. Below are the steps that are used to build the program.

  • Import os library consists of required methods to list the files in a directory.
  • Use the os.listdir(‘path’) method, which accepts the specific directory path to return the list of files and directories.

Below is a simple program that lists files and directories in a specified directory.

Code:

# import required libraries import os 
# list that stores file & directory names in the specified path
listOfNames = os.listdir(r'C:\Users\Akhil\programs')
print(listOfNames)

Output

sub_programs
dataContent.txt
simpleCode.py
listFileNames.py

Note: os. path.isfile() method is used to distinguish a file from a directory.

So the isfile() method can get only a list of files in the specified directory path.

Code:

# import required libraries 
import os
# path
folderPath = r'C:\Users\Akhil\programs'
listofFiles = [] for file_dir in os.listdir(folderPath):
# checking whether its a file/directory
if os.path.isfile(os.path.join(folderPath, file_dir)):
listofFiles.append(file_dir)
for f in listofFiles:
print(f)

Output

dataContent.txt
simpleCode.py
listFileNames.py

Also Read: Sending Emails Using Python With Image And PDF Attachments

Method 2: Print all files in a directory using the walk() method

The walk() function is present in the os library, which lists the files, directories, and subdirectories. It recursively iterates over the directory to list all the files and directories until no sub-directories are available in the given directory.

The os.walk() method returns a generator that creates a tuple of values (current path, directories in current path, files in current path)

Below is an example program that lists only files in the specified directory (also explores the files in sub-directories) using the os.walk() method.

Code:

# import required libraries 
import os
# path
folderPath = r'C:\Users\Akhil\programs'
listofFiles = []
for (folderPath, directories, files) in os.walk(folderPath):
# storing only file info listofFiles.extend(files)
for f in listofFiles:
print(f)

Output

Example1.py
dataContent.txt
simpleCode.py
listFileNames.py

Also Read: Python For Loop Index With Examples

Method 3: Print all files in a directory using the scandir() method

The scandir() method gives directory entries and file attribute information. It is present in the os library. The os.scandir() method accepts the path i.e., the directory in which files must be listed.

Example:

# import required libraries 
import os
# path
folderPath = r'C:\Users\Akhil\programs'
for file_dir in os.scandir(folderPath):
# checking if file_dir is a file
if(file_dir.is_file()):
print(file_dir.name)

Output

dataContent.txt
simpleCode.py
listFileNames.py

Also Read: How To Use Python For Browser Games Development?

Method 4: Print all files in a directory using the glob module

The glob module in Python finds the files and folders (directories) which follow a specific pattern.

In the example below, we list all files in the given directory.

Example:

# import required libraries 
import glob
listofFiles = glob.glob(r'C:\Users\Akhil\programs\*.*')
for fi in listofFiles:
print(fi)

Output

C:\Users\Akhil\programs\dataContent.txt
C:\Users\Akhil\programs\simpleCode.py
C:\Users\Akhil\programs\listFileNames.py

Also Read: How to Exit Python Program [4 Methods]

Method 5: Print all files in a directory using the pathlib library

The pathlib library in Python also helps us list all files in a directory. Below are the steps used to list the files in a directory.

  • Import pathlib library
  • Define the directory path using the path() method from the pathlib library.
  • Iterate over each file and directory in the above-defined path using the iterdir() method.
  • We can filter only files using the isfile() function.

Below is an example program that was built using the above steps.

Example:

# import required libraries 
import pathlib
# define path
diectory_path = pathlib.path(r'C:\Users\Akhil\programs\')
# iterate through directory
for file_dir in diectory_path.iterdir():
if file_dir.is_file():
print(file_dir)

Output

dataContent.txt
simpleCode.py
listFileNames.py

Also Read: Increment and Decrement Operators in Python

Conclusion

In this article, we have seen multiple ways to list all files in a detailed directory with an example program. You can print all files in a directory using Python by any of the methods mentioned in this tutorial.


How to Print All Files in a Directory Using Python [5 Methods] was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Himanshu Tyagi

In this blog, you will learn how to print all files in a directory using Python. Python allows us to list all files inside a directory/folder in multiple ways. The ways to list all files in a directory are listed below:

  • listdir() method
  • walk() method
  • scandir() method
  • Using glob library
  • Methods from the pathlib library

Let’s look into each method in detail with an example program.

Also Read: How To Copy List in Python

Python Print All Files in a Directory

Method 1: Print all files in a directory using the listdir() method

The listdir() method returns the list of files and directories in the specified path. The listdir() method accepts an argument that represents the directory path. Below are the steps that are used to build the program.

  • Import os library consists of required methods to list the files in a directory.
  • Use the os.listdir(‘path’) method, which accepts the specific directory path to return the list of files and directories.

Below is a simple program that lists files and directories in a specified directory.

Code:

# import required libraries import os 
# list that stores file & directory names in the specified path
listOfNames = os.listdir(r'C:\Users\Akhil\programs')
print(listOfNames)

Output

sub_programs
dataContent.txt
simpleCode.py
listFileNames.py

Note: os. path.isfile() method is used to distinguish a file from a directory.

So the isfile() method can get only a list of files in the specified directory path.

Code:

# import required libraries 
import os
# path
folderPath = r'C:\Users\Akhil\programs'
listofFiles = [] for file_dir in os.listdir(folderPath):
# checking whether its a file/directory
if os.path.isfile(os.path.join(folderPath, file_dir)):
listofFiles.append(file_dir)
for f in listofFiles:
print(f)

Output

dataContent.txt
simpleCode.py
listFileNames.py

Also Read: Sending Emails Using Python With Image And PDF Attachments

Method 2: Print all files in a directory using the walk() method

The walk() function is present in the os library, which lists the files, directories, and subdirectories. It recursively iterates over the directory to list all the files and directories until no sub-directories are available in the given directory.

The os.walk() method returns a generator that creates a tuple of values (current path, directories in current path, files in current path)

Below is an example program that lists only files in the specified directory (also explores the files in sub-directories) using the os.walk() method.

Code:

# import required libraries 
import os
# path
folderPath = r'C:\Users\Akhil\programs'
listofFiles = []
for (folderPath, directories, files) in os.walk(folderPath):
# storing only file info listofFiles.extend(files)
for f in listofFiles:
print(f)

Output

Example1.py
dataContent.txt
simpleCode.py
listFileNames.py

Also Read: Python For Loop Index With Examples

Method 3: Print all files in a directory using the scandir() method

The scandir() method gives directory entries and file attribute information. It is present in the os library. The os.scandir() method accepts the path i.e., the directory in which files must be listed.

Example:

# import required libraries 
import os
# path
folderPath = r'C:\Users\Akhil\programs'
for file_dir in os.scandir(folderPath):
# checking if file_dir is a file
if(file_dir.is_file()):
print(file_dir.name)

Output

dataContent.txt
simpleCode.py
listFileNames.py

Also Read: How To Use Python For Browser Games Development?

Method 4: Print all files in a directory using the glob module

The glob module in Python finds the files and folders (directories) which follow a specific pattern.

In the example below, we list all files in the given directory.

Example:

# import required libraries 
import glob
listofFiles = glob.glob(r'C:\Users\Akhil\programs\*.*')
for fi in listofFiles:
print(fi)

Output

C:\Users\Akhil\programs\dataContent.txt
C:\Users\Akhil\programs\simpleCode.py
C:\Users\Akhil\programs\listFileNames.py

Also Read: How to Exit Python Program [4 Methods]

Method 5: Print all files in a directory using the pathlib library

The pathlib library in Python also helps us list all files in a directory. Below are the steps used to list the files in a directory.

  • Import pathlib library
  • Define the directory path using the path() method from the pathlib library.
  • Iterate over each file and directory in the above-defined path using the iterdir() method.
  • We can filter only files using the isfile() function.

Below is an example program that was built using the above steps.

Example:

# import required libraries 
import pathlib
# define path
diectory_path = pathlib.path(r'C:\Users\Akhil\programs\')
# iterate through directory
for file_dir in diectory_path.iterdir():
if file_dir.is_file():
print(file_dir)

Output

dataContent.txt
simpleCode.py
listFileNames.py

Also Read: Increment and Decrement Operators in Python

Conclusion

In this article, we have seen multiple ways to list all files in a detailed directory with an example program. You can print all files in a directory using Python by any of the methods mentioned in this tutorial.


How to Print All Files in a Directory Using Python [5 Methods] was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Himanshu Tyagi


Print Share Comment Cite Upload Translate Updates
APA

Himanshu Tyagi | Sciencx (2023-04-23T14:48:09+00:00) How to Print All Files in a Directory Using Python [5 Methods]. Retrieved from https://www.scien.cx/2023/04/23/how-to-print-all-files-in-a-directory-using-python-5-methods/

MLA
" » How to Print All Files in a Directory Using Python [5 Methods]." Himanshu Tyagi | Sciencx - Sunday April 23, 2023, https://www.scien.cx/2023/04/23/how-to-print-all-files-in-a-directory-using-python-5-methods/
HARVARD
Himanshu Tyagi | Sciencx Sunday April 23, 2023 » How to Print All Files in a Directory Using Python [5 Methods]., viewed ,<https://www.scien.cx/2023/04/23/how-to-print-all-files-in-a-directory-using-python-5-methods/>
VANCOUVER
Himanshu Tyagi | Sciencx - » How to Print All Files in a Directory Using Python [5 Methods]. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/23/how-to-print-all-files-in-a-directory-using-python-5-methods/
CHICAGO
" » How to Print All Files in a Directory Using Python [5 Methods]." Himanshu Tyagi | Sciencx - Accessed . https://www.scien.cx/2023/04/23/how-to-print-all-files-in-a-directory-using-python-5-methods/
IEEE
" » How to Print All Files in a Directory Using Python [5 Methods]." Himanshu Tyagi | Sciencx [Online]. Available: https://www.scien.cx/2023/04/23/how-to-print-all-files-in-a-directory-using-python-5-methods/. [Accessed: ]
rf:citation
» How to Print All Files in a Directory Using Python [5 Methods] | Himanshu Tyagi | Sciencx | https://www.scien.cx/2023/04/23/how-to-print-all-files-in-a-directory-using-python-5-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.