[Introduction to Python] How to get data with the listdir function

Reference site: [Introduction to Python] How to get data with listdir function

[Introduction to Python] How to get data with the listdir function

Python allows you to read a specific file and get the data. You can type in the file name directly, but there are times when you need to get a list of files and select a file in your program. In that case, you can easily get a list of directories (folders) and files by using the listdir function.

This time, I will explain how to get the directory and file list and path using the listdir function.

table of contents 1 [Get a list of directories and files with the listdir function](## Get a list of directories and files with the listdir function) 2 [Get only what you need in combination with the lisdir function](## Get only what you need in combination with the lisdir function) 3 [Get the file with the glob function](## Get the file with the glob function)

Get a list of directories and files with the listdir function

Use the listdir function to get a list of directories and files in Python. The syntax of the listdir function is as follows.

import os  #Must be required

List variable= os.listdir('path')

The istdir function is a function of the module named os, so be sure to import it when using the listdir function. By passing the path of the directory and the directory for which you want to get the file list to the argument of listdir, you can retrieve the list of directories and files in that directory.

import os

directory = os.listdir('C:\Python35')
print(directory)

Execution result

[‘Lib’, ‘python.exe’, ‘Readme.txt’, ‘Scripts’, ‘tcl’, ‘test.txt’] Get only what you need in combination with the lisdir function

Get only what you need in combination with the isdir function

The listdir function will get a list of all the folders and files in the specified path. However, in some cases you may want only directories, or vice versa. In such a case, it can be realized by using isdir which can check whether the specified path points to a directory or a file. The syntax of isdir is as follows.

os.path.isdir('path')

isdir returns True if the path passed as an argument is a directory, False otherwise. You can use this with listdir to get a list of just directories or just files.

import os
 
path = 'C:\Python35\\'  #The directory for which you want to get the directory list
files = []
 
for x in os.listdir(path):  
    if os.path.isdir(path + x):  #Add the extracted objects to the path to make the full path
        files.append(x)
    
print(files)

Execution result

[‘Lib’, ‘Scripts’, ‘tcl’]

First, get a list of directories and files with listdir, and retrieve them one by one with a for statement. By concatenating the extracted directory name or file name after the original path, you can create the path of that directory or file.

Pass that path to isdir and you'll know if it's a directory or a file. In this case, I try to add only those that isdir returns True, that is, the directories. By denying this if statement, you can retrieve only the file statement.

If you use isfile instead of isdir, you can retrieve only the file.

import os

path = 'C:\Python35\\'
files = []

for x in os.listdir(path):
    if os.path.isfile(path + x):  #use isfile instead of isdir
        files.append(x) 
    
print(files)

Execution result

[‘python.exe’, ‘Readme.txt’, ‘test.txt’]

Get the file with the glob function

You can now get a list of directories and files by using listdir, isdir and isfile. However, there are times when you want to get the file with more detail. For example, if you want to extract a file with only the .txt extension, you have to manipulate the string after extracting it.

import os

path = 'C:\Python35\\'
files = []
texts = []

for x in os.listdir(path):
    if os.path.isfile(path + x):
        files.append(x) 
    
for y in files:
    if(y[-4:] == '.txt'):     #Extract the last 4 characters of the file name and that is.If txt
        texts.append(y)  #Add to list
        
print(texts)

Execution result

[‘Readme.txt’, ‘test.txt’]

However, this method complicates the code and the length differs depending on the extension, so it is troublesome to set it every time. In such cases, using the glob function instead of listdir will make it cleaner. The syntax of glob is as follows.

import os

path = 'C:\Python35\\'
files = []
texts = []

for x in os.listdir(path):
    if os.path.isfile(path + x):
        files.append(x) 
    
for y in files:
    if(y[-4:] == '.txt'):     #Extract the last 4 characters of the file name and that is.If txt
        texts.append(y)  #Add to list
        
print(texts)

Be sure to import glob because the glob function is a function of the glob module. The glob function also passes a path as an argument, but unlike listdir, you can enter not only a single directory name but also a single file name. In addition, wildcards (*) and regular expressions that mean arbitrary character strings can be used as arguments of glob, so you can easily get only a specific file.

import glob

path = 'C:\Python35\\*.txt'
files = []

files = glob.glob(path)

print(files)

Execution result

[‘C:\Python35\Readme.txt’, ‘C:\Python35\test.txt’]

In this example, "* .txt" is specified for path, so only files with the extension .txt are extracted. If you use the glob function, you can get the file in any pattern by devising the path specification method.

Recommended Posts

[Introduction to Python] How to get data with the listdir function
[Introduction to Python] How to get the index of data with a for statement
[Introduction to Python] How to split a character string with the split function
[Introduction to Python] How to write a character string with the format function
How to get the Python version
How to get started with Python
How to get into the python development environment with Vagrant
[Python] Explains how to use the format function with an example
I tried to get CloudWatch data with Python
[Introduction to Python] How to handle JSON format data
Introduction to Python with Atom (on the way)
How to get the files in the [Python] folder
[Python] Explains how to use the range function with a concrete example
How to get the date and time difference in seconds with python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Get Youtube data with python
How to get the variable name itself in python
How to get the number of digits in Python
How to scrape image data from flickr with python
[Python] How to specify the download location with youtube-dl
Reading Note: An Introduction to Data Analysis with Python
How to get more than 1000 data with SQLAlchemy + MySQLdb
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
How to get mouse wheel verdict with Python curses
[Introduction to Python] How to stop the loop using break?
How to get started with the 2020 Python project (windows wsl and mac standardization)
Note: How to get the last day of the month with python (added the first day of the month)
How to get a list of files in the same directory with python
I tried to simulate how the infection spreads with Python
[Python] How to FFT mp3 data
Try to get the function list of Python> os package
Python: How to use async with
How to use the zip function
Link to get started with python
Minimum knowledge to get started with the Python logging module
How to deal with imbalanced data
Get the weather with Python requests
How to deal with imbalanced data
[Python] Get economic data with DataReader
How to get started with Scrapy
[Python] How to import the library
How to get started with Django
How to get the last (last) value in a list in Python
How to Data Augmentation with PyTorch
How to use FTP with Python
How to calculate date with python
How to use python zip function
Get additional data to LDAP with python (Writer and Reader)
Get the source of the page to load infinitely with python.
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
How is the progress? Let's get on with the boom ?? in Python
python / pandas / dataframe / How to get the simplest row / column / index / column
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to get followers and followers from python using the Mastodon API
Introduction to Data Analysis with Python P32-P43 [ch02 3.US Baby Names 1880-2010]
[Python] How to get the first and last days of the month
Introduction to Data Analysis with Python P17-P26 [ch02 1.usa.gov data from bit.ly]
How to get the directory where the EXE built with Pyinstaller exists