How to get the files in the [Python] folder

Use glob to get the files in a folder with python

When the following files are in the test1 folder

test1.log test2.log test3.log test1.txt test2.txt test3.txt test4.txt test10.txt test20.txt test30.txt

Get all files

In [1]: import glob

In [2]: glob.glob('./test1/*')
Out[2]: 
['./test1/test2.log',
 './test1/test20.txt',
 './test1/test1.txt',
 './test1/test2.txt',
 './test1/test10.txt',
 './test1/test30.txt',
 './test1/test3.log',
 './test1/test3.txt',
 './test1/test4.txt',
 './test1/test1.log']

Get only the extension txt

In [3]: glob.glob('./test1/*.txt')
Out[3]: 
['./test1/test20.txt',
 './test1/test1.txt',
 './test1/test2.txt',
 './test1/test10.txt',
 './test1/test30.txt',
 './test1/test3.txt',
 './test1/test4.txt']

Get a file with 2 digits after test

In [4]: glob.glob('./test1/test[0-9][0-9].*')
Out[4]: ['./test1/test20.txt', './test1/test10.txt', './test1/test30.txt']

Recommended Posts

How to get the files in the [Python] folder
[Python] Get the files in a folder with Python
How to get the Python version
How to get the variable name itself in python
How to get the number of digits in Python
How to get a stacktrace in python
How to get the last (last) value in a list in Python
How to get a list of files in the same directory with python
Summary of how to import files in Python 3
How to develop in Python
How to get the date and time difference in seconds with python
How to retrieve the nth largest value in Python
How to know the current directory in Python in Blender
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
How to use the model learned in Lobe in Python
[Python] How to output the list values in order
[Python] How to do PCA in Python
How to collect images in Python
Get the desktop path in Python
How to use SQLite in Python
Get the script path in Python
In the python command python points to python3.8
How to get started with Python
[Python] How to import the library
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
Get the desktop path in Python
Get the host name in Python
How to handle Japanese in Python
[python] How to check if the Key exists in the dictionary
How to use the __call__ method in a Python class
I tried "How to get a method decorated in Python"
How to get all the keys and values in the dictionary
How to get a list of built-in exceptions in python
How to get into the python development environment with Vagrant
[Shell] How to get the remote default branch in Git
[Introduction to Python] How to get data with the listdir function
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
Read all csv files in the folder
How to extract polygon area in Python
How to check opencv version in python
How to get colored output to the console
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
Get the EDINET code list in Python
How to display Hello world in python
How to read CSV files in Pandas
How to use is and == in Python
How to write Ruby to_s in Python
How is the progress? Let's get on with the boom ?? in Python
How to determine the existence of a selenium element in Python
How to know the internal structure of an object in Python