How to get a list of files in the same directory with python

The __glob module __ is useful for getting a list of files in the same directory.

Example of using glob module

Try running glob in the directory where the following files are saved.  data1.txt, data10.txt, data2.txt, data3.dat, data99.txt, result1.txt, result2.dat

・ Use wild card

You can get a list of all the files in the same directory by the following process.

import glob

filelist=glob.glob('*')
print(filelist)

Execution result

['data1.txt', 'data10.txt', 'data2.txt', 'data3.dat', 'data99.txt', 'result1.txt', 'result2.dat']

You can use the wildcard'*' to list only filenames that meet certain criteria. In the example below, the files whose file names start with'data'and those whose extension is'.txt' are listed.

filelist=glob.glob('data*') #'data'List all file names that start with
print(filelist)
filelist=glob.glob('*.txt') #'.txt'List all filenames ending in
print(filelist)

Execution result

['data1.txt', 'data10.txt', 'data2.txt', 'data3.dat', 'data99.txt']
['data1.txt', 'data10.txt', 'data2.txt', 'data99.txt', 'result1.txt']
・ Use'?'

If you want to specify the number of characters in the wildcard part, use'?' Instead of'*'. Connect as many'?' As the number of characters you want to specify. The following example can list files with any two characters between'data' and'.txt'.

filelist=glob.glob('data??.txt') #'data'When'.txt'List all files that contain any two characters between
print(filelist)

Execution result

['data10.txt', 'data99.txt']
・ Use []

You can also use []. In this case, the files enclosed in [] that match any one of the alphanumeric characters in [] are listed.

filelist=glob.glob('data[0-9].txt') #[0-9]List all items that have a number from 0 to 9 in the part of
print(filelist)

Execution result

['data1.txt', 'data2.txt']

Recommended Posts

How to get a list of files in the same directory with python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
[Python] Get the files in a folder with Python
How to get the files in the [Python] folder
Get a list of files in a folder with python without a path
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to get the number of digits in Python
How to pass the execution result of a shell command in a list in Python
[Introduction to Python] How to get the index of data with a for statement
Get the number of specific elements in a python list
How to get a stacktrace in python
Get the value of a specific key in a list from the dictionary type in the list with Python
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
How to determine the existence of a selenium element in Python
Try to get a list of breaking news threads in Python.
How to check the memory size of a dictionary in Python
How to clear tuples in a list (Python)
Summary of how to import files in Python 3
Get the caller of a function in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
Make a copy of the list in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
Get a list of packages installed in your current environment with python
How to get the date and time difference in seconds with python
[Python] How to put any number of standard inputs in a list
[Linux] Command to get a list of commands executed in the past
Receive a list of the results of parallel processing in Python with starmap
How to format a list of dictionaries (or instances) well in Python
How to convert / restore a string with [] in python
How to get the variable name itself in python
How to know the current directory in Python in Blender
[python] Get the list of classes defined in the module
I want to see a list of WebDAV files in the Requests module
Note: How to get the last day of the month with python (added the first day of the month)
How to get the Python version
How to get started with Python
[Ubuntu] How to delete the entire contents of a directory
Try to get the function list of Python> os package
[Python] How to make a list of character strings character by character
How to shuffle a part of a Python list (at random.shuffle)
How to use the __call__ method in a Python class
[Command] Command to get a list of files containing double-byte characters
Get a list of purchased DMM eBooks with Python + Selenium
How to develop in a virtual environment of Python [Memo]
[Note] Import of a file in the parent directory in Python
How to display a list of installable versions with pyenv
How to get into the python development environment with Vagrant
How to get a list of links from a page from wikipedia
How to get a quadratic array of squares in a spiral!
Get the source of the page to load infinitely with python.
How to connect the contents of a list into a string
[Note] How to write QR code and description in the same image with python
How to find the first element that matches your criteria in a Python list
I wanted to know the number of lines in multiple files, so I tried to get it with a command
How to display a specified column of files in Linux (awk)
How is the progress? Let's get on with the boom ?? in Python
How to handle multiple versions of CUDA in the same environment
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)