Reference site: [Introduction to Python] How to get data with 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)
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
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’]
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