[PYTHON] Get the file name in a folder using glob

It is often used when you want to easily get the file name in a folder. Click here for the source http://svn.python.org/view/python/branches/release27-maint/Lib/glob.py?view=markup If you have a folder like the one below

terminal.


$ ls
0.csv		2.txt		5.csv		7.txt		test_glob.py
0.txt		3.csv		5.txt		8.csv
1.csv		3.txt		6.csv		8.txt
1.txt		4.csv		6.txt		9.csv
2.csv		4.txt		7.csv		9.txt
sample

test_glob.py


   import glob
   text_fnames = glob.glob('./*.txt')
   print text_fnames
Run

output.


$ python test_glob.py 
['./0.txt', './1.txt', './2.txt', './3.txt', './4.txt', './5.txt', './6.txt', './7.txt', './8.txt', './9.txt']

In this way, you can easily get the txt file in the file by setting it to * .txt.

Commentary

The argument of glob.glob is a string, and it is written in the same way as the shell. Both relative and absolute paths can be used as arguments.

Also, if you change glob.glob to glob.iglob, the iterator will come back. Unless the number of files is huge, I think that glob.glob is enough.

In the source content, glob.iglob creates file names one by one with yeild. glob.glob (pathname) is just like list (glob.iglob (pathname)). The rest is a simple code to search using the  Ľos` module.

Recommended Posts

Get the file name in a folder using glob
[Python] Get the files in a folder with Python
Get the host name in Python
Get the file path using Pathlib
Get the file name saved in AWS S3 (1000 or more)
Get the filename of a directory (glob)
Get the formula in an excel file as a string in Python
Get the hierarchy name using the OpenMaya iterator
Format the Git log and get the committed file name in csv format
Replace the directory name and the file name in the directory together with a Linux command.
A memo organized by renaming the file names in the folder with python
Extract only the file name excluding the directory in the directory
Get the caller of a function in Python
Get only the subclass elements in a list
How to get the files in the [Python] folder
Create a MIDI file in Python using pretty_midi
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
How to get the notebook name you are currently using in Google Colab
Save the setting conditions as a CSV file using UDF Manager in OCTA
Replace the named entity in the read text file with a label (using GiNZA)
Don't forget to close the file just because it's in a temporary folder
I want to get the file name, line number, and function name in Python 3.4
How to get the variable name itself in python
Get the variable name of the variable as a character string.
Get a participant's username and screen name in Slack
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
Download the file in Python
[Sublime Text 2] Always execute a specific file in the project
Save the pystan model and results in a pickle file
[Python] Open the csv file in the folder specified by pandas
[python] Change the image file name to a serial number
Change the standard output destination to a file in Python
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list
Process the files in the folder in order with a shell script
How to generate a query using the IN operator in Django
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
Get the class name where the method is defined in the decorator
Recursively get the Excel list in a specific folder with python and write it to Excel.
Output the key list included in S3 Bucket to a file
I get a UnicodeDecodeError in mecab-python3
Count specific strings in a file
Process the contents of the file in order with a shell script
File / folder path manipulation in Python
Save the binary file in Python
How to get all the possible values in a regular expression
Search the file name including the specified word and extension in the directory
I get a KeyError in pyclustering.xmeans
Get the desktop path in Python
Anyway, the fastest serial communication log is left in a file
Get the script path in Python
Create a binary file in Python
Save the Pydrive authentication file in a different directory from the script
Sort the file names obtained by Python glob in numerical order
Settings for running a test each time you save a file in the editor using watchmedo (watchdog)
The story of the "hole" in the file
Read a file in Python with a relative path from the program
How to get the vertex coordinates of a feature in ArcPy
Extract the targz file using python
I get a can't set attribute when using @property in python