How to Read Files From a Directory in Python

The Python os library is used to listing the files in a directory. The Python os.listdir() method returns a list of every file and folder in a directory. os.walk() function returns a list of every file in an unabridged file tree.


Often, when you're working with files in Python, yous'll encounter situations where you want to listing the files in a directory. For case, you may want to find all of the Python files in a folder.

The Python os library offers a number of methods that can be used to list files in a directory. This tutorial volition hash out how to utilize bone.listdir() to get the files and folders in a director. We'll also talk about using bone.walk() to get the files and folders in a directory and in its subdirectories.

Python os Library

The Python bone library provides a number of functions that you can use to work with operating systems. The functions included in the bone module work on any mod operating system, whether information technology is Windows, Linux, or Mac.

Since os is an external library, nosotros need to import it into our code before we start using it. We can do so using a Python import statement:

Now that we've imported the os library into our code, we tin start using its functions to listing items in a directory.

Python os.listdir()

In Python, the os.listdir() method lists files and folders in a given directory. The method does not return special entries such as '.' and '..', which the operating system uses to navigate through unlike directories.

os.listdir() also does not render files and folders across the outset level of folders. In other words, bone.listdir() does not return annihilation within subfolders discovered by the method.

81% of participants stated they felt more confident near their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.

The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their start chore.

The os.listdir() function accepts one parameter: the file path of the directory whose file and binder names y'all want to call up.

Here's the syntax for the listdir method:

Let'south walk through an instance to showcase how to employ this method in a Python program.

os.listdir() Python Case

Say that we are creating a program that analyzes the stock market performance of Netflix over the last decade. We take a folder (name: /home/data_analysis/netflix) with all of our raw data, and before our programme starts running, we want to bank check to make sure that the file raw_data_2019.csv exists within that folder.

In order to function properly, our programme needs that particular file to be stored in that item folder.

We could use the post-obit lawmaking to retrieve a list of the files in the /abode/data_analysis/netflix piece of work directory:

import bone  path = '/home/data_analysis/netflix'  files = os.listdir(path)  for f in files: 	impress(f)          

Our plan retrieves a list of all files and folders in the specified directory and returns the post-obit:

form-submission

Find Your Bootcamp Friction match

  • Career Karma matches yous with top tech bootcamps
  • Get exclusive scholarships and prep courses
README.md app.py raw_data_2016.csv raw_data_2017.csv raw_data_2018.csv raw_data_2019.csv processed_data          

At present, we tin check to come across if the file raw_data_2019.csv is in the binder. As y'all tin see, it is.

Allow's break down our code. On the first line, we import the os module, which we need to practice in social club to admission the os.listdir() function. So, we declare a Python variable chosen path, which stores the name of the path whose contents we desire to retrieve.

On the next line, nosotros apply the os.listdir() method to go a list of the files and folders in the /home/data_analysis/netflix directory. Finally, we create a Python for loop. This loop iterates through every item in the list produced by os.listdir(). We impress out the name of each file to the panel using a Python print() argument.

The /abode/data_analysis/netflix directory independent half dozen files and ane directory. The directory is called processed_data and is distinguishable from the other files because it does not have an extension.

Python os.walk()

The bone.walk() office retrieves a list of files contained within a tree. The method iterates over each directory in a tree. Then, bone.walk() returns the name of every file and folder within a directory and whatever of its subdirectories.

The syntax for the os.walk() method is every bit follows:

bone.walk(tiptop, topdown, onerror, followlinks)

The os.walk() method accepts iv parameters:

  • peak is the tiptop directory whose component file and folder names you want to remember (required)
  • topdown, when set to Truthful, specifies that directories should be scanned from the top down. If this value is set to False, directories will exist scanned from the lesser upwards (optional)
  • onerror provides an error handler if an mistake is encountered (optional)
  • followlinks, if set to True, visits folders referenced by system links (optional)

We are going to focus on the first two parameters since onerror and followlinks are more avant-garde and are not equally ordinarily used.

os.walk() Python Case

Let'southward say that we want to retrieve the names of all files in the /dwelling house/data_analysis/netflix directory. We also desire to find out what's enclosed inside all subdirectories in that folder.

As we discussed above, the netflix directory contains one folder: processed_data. We could use the post-obit lawmaking to remember the names of all files in the /home/data_analysis/netflix directory and its subdirectories:

import os  path = '/habitation/data_analysis/netflix'  for root, directories, files in os.walk(path, topdown=Imitation): 	for name in files: 		print(os.path.bring together(root, name)) 	for name in directories: 		print(os.path.bring together(root, proper noun))          

Here'due south the output from our code:

Venus profile photo

"Career Karma entered my life when I needed it about and quickly helped me match with a bootcamp. Two months later graduating, I institute my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

/domicile/data_analysis/netflix/README.medico /home/data_analysis/netflix/app.py /dwelling house/data_analysis/netflix/raw_data_2016.csv /home/data_analysis/netflix/raw_data_2017.csv /dwelling/data_analysis/netflix/raw_data_2018.csv /home/data_analysis/netflix/raw_data_2019.csv /domicile/data_analysis/netflix/processed_data /home/data_analysis/netflix/processed_data/last.csv          

Nosotros import the os module from which we reference the os.walk() and os.path.join() methods later in our code. Then, nosotros declare a variable called path, which stores the path whose file names nosotros want to discover.

We and so create a for loop that uses os.walk() to retrieve a listing of all files and folders in the path directory. That loop iterates through the files and folders that bone.walk() returns. It's worth noting that nosotros specify the topdown=Fake parameter in the os.walk() method, which tells our code to acquit a top-down search.

Our for loop iterates through each file and directory discovered by the bone.walk() method using additional for loops. We impress out the files in os.walk() to the console.

In our code higher up, here are our for loops:

for root, directories, files in os.walk(path): 	for name in files: 		impress(bone.path.join(root, name)) 	for proper noun in directories: 		impress(os.path.join(root, name))

So, our program uses os.path.bring together() to join together the root folder of each file (i.e. /habitation/data_analysis/netflix)and the name of the file (i.e. raw_datra_2019.csv). The root folder refers to the directory path in which a file exists.

Conclusion

You tin utilise the Python listdir() method to do this. You lot can besides use the walk() method, which lists everything in a directory, including anything inside subdirectories.

This guide explored, providing examples, how to apply the os.listdir() and os.walk() methods to list files and folders in a directory in Python. At present y'all take the skills you lot need to list files in a directory in Python like an expert!

To learn more near coding in Python, read our full How to Acquire Python guide.

everhartparealeareed.blogspot.com

Source: https://careerkarma.com/blog/python-list-files-in-directory/

0 Response to "How to Read Files From a Directory in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel