Open All Files in a Folder in Python

Open All Files in a Folder in Python

  1. Open All Files in a Folder Using os.walk() in Python
  2. Open All Files in a Folder Using os.listdir() in Python
  3. Conclusion

A directory, also known as a folder, is a unit organizational structure in a computer file system used to store and locate files or folders. Python has several APIs for listing the contents of a directory.

There are many built-in modules and methods for file handling in Python. Some of these functions can be found in OS, Glob, and more modules.

Open All Files in a Folder Using os.walk() in Python

Various OS modules in Python programming allow multiple methods to interact with the file system. It has a walk() function that will enable us to list all the files in a specific path by traversing the directory either bottom-up or top-down and returning three tuples - root, dir, and files.

Syntax:

os.walk(r'pathname')

In the above syntax, r is to read the root folder or directory, and the parameter pathname is the path of the folder.

Example:

import os
for root, dirs, files in os.walk(r'/content/drive/MyDrive/Skin Cancer'):
    for file in files:
        if file.endswith('.zip'):
            print(os.path.join(root, file))

In the code, we first imported the OS module. Then in the read mode, we used a for loop and passed the pathname to the walk function.

The loop iterates through all files that meet the file extension condition. The above code will read all files with a .zip extension.

Output:

/content/drive/MyDrive/Skin Cancer/archive.zip

As you can see, the Google drive Skin Cancer folder contains one zip file.

Open All Files in a Folder Using os.listdir() in Python

listdir() is another method in OS that helps us find files on a specific path. It returns all the files in the directory specified by the location or path in random order as a list format.

It ignores the . and .. characters if they are present in the input folder.

Syntax:

os.listdir(r'pathname')

In the above syntax, r is to read the root folder or directory, and the parameter pathname is the path of the folder.

Example:

import os
for file in os.listdir(r'/content/drive/MyDrive/Machine learning'):
    if file.endswith(".csv"):
        print(os.path.join(r'/content/drive/MyDrive/Machine learning', file))

The above code first imported the OS module. The loop returns and prints the name of the files.

Output:

/content/drive/MyDrive/Machine learning/Perceptrony.csv
/content/drive/MyDrive/Machine learning/PerceptronX.csv
/content/drive/MyDrive/Machine learning/songs.csv
/content/drive/MyDrive/Machine learning/cardio_train.csv

Open All Files in a Folder Using glob.glob() in Python

In the previous examples, we had to iterate through a list of files in a directory whose names matched the specific extensions or patterns. However, glob modules allow you to find a list of files with specific extensions or patterns.

Syntax:

glob.glob(path name, recursive=True)

There are two parameters to this function.

  • The first is path name with a specific pattern, which filters out all files and returns a list.
  • Another parameter called recursive is set to off by default, which means false. When its value is true, the function searches its directory and subdirectories.

All wild cards, such as ?, *, and so on, are permitted here.

Example:

import glob
import os
for file in glob.glob(r'/content/drive/MyDrive/Machine learning' + "**/*.csv", recursive=True):
    print(os.path.join(r'/content/drive/MyDrive/Machine learning', file))

We imported two python modules in the above code - glob and os. We use a for loop and glob.glob() function to open all files with .csv extension in files

Conclusion

This article discussed various methods for opening all files in a folder with an extension. The methods we used are the walk() and listdir() methods of the os module, as well as the glob() method of the glob module.

Related Article - Python File

  • Get All the Files of a Directory
  • Delete a File and Directory in Python
  • Append Text to a File in Python
  • Check if a File Exists in Python
  • Find Files With a Certain Extension Only in Python
  • Read Specific Lines From a File in Python