Get the filename of a directory (glob)

1 What is this article?

Explains how to get the file name containing the specified word (eg .htm) from the files in a certain folder.

2 How to write code

This is the code to extract the files in the directory specified in filepath that include ".htm" in the file name.

test.py


import glob

filepath = 'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc/'

files = glob.glob(filepath+'*.htm') #Get htm file
files = sorted(files) #Just sorting the files
print(files)

★★ Execution result ★★

python


['C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0000000_header_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0101010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0102010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0103010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0104010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105000_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105020_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105025_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105040_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105050_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105100_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105110_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105310_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105320_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105330_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105400_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0105410_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0106010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0107010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm', 
'C:/Users/fdfpy/work/yuho/stuck/XBRL/PublicDoc\\0201010_honbun_jpcrp030000-asr-001_E02995-000_2019-08-20_01_2019-11-08_ixbrl.htm']

Recommended Posts

Get the filename of a directory (glob)
Get the caller of a function in Python
Get the variable name of the variable as a character string.
Get the file name in a folder using glob
Get the number of digits
How to get a list of files in the same directory with python
[Ubuntu] How to delete the entire contents of a directory
Get the id of a GPU with low memory usage
Get UNIXTIME at the beginning of today with a command
Get the number of specific elements in a python list
Get the number of views of Qiita
Get the attributes of an object
Get the first element of queryset
Get the number of Youtube subscribers
The story of writing a program
[Python] Get the update date of a news article from HTML
Get the URL of a JIRA ticket created with the jira-python library
How to get the vertex coordinates of a feature in ArcPy
Dig the directory and create a list of directory paths + file names
Create a function to get the contents of the database in Go
Get the number of readers of a treatise on Mendeley in Python
Get the column list & data list of CASTable
Measure the relevance strength of a crosstab
A quick overview of the Linux kernel
Get the minutes of the Diet via API
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Get the value of the middle layer of NN
A memo explaining the axis specification of axis
[Python] Get the character code of the file
The story of blackjack A processing (python)
[Python] Get a list of folders only
[PowerShell] Get the reading of the character string
Notice the completion of a time-consuming command
For Windows: Get a list of directories and files under a specific directory.
Get a capture of the entire web page in Selenium Python VBA
Get the number of searches with a regular expression. SeleniumBasic VBA Python
[Linux] Command to get a list of commands executed in the past
View the full path (absolute path) of a file in a directory in Linux Bash
Get a datetime instance at any time of the day in Python
Get the average salary of a job with specified conditions from indeed.com
How to calculate the volatility of a brand
Get a lot of Twitter tweets at once
I want to get the path of the directory where the running file is stored.
Get the contents of git diff from python
[Python] Get the files in a folder with Python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Visualize the inner layer of a neural network
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
Make a copy of the list in Python
Find the number of days in a month
Get only the subclass elements in a list
I get a strange window when I use the open directory dialog in Tkinter
A note about the python version of python virtualenv
[Python] Get the last updated date of the website
The story of making a lie news generator
Use twitter API to get the number of tweets related to a certain keyword
Get a list of Qiita likes by scraping
[Python] A rough understanding of the logging module
Python script to get a list of input examples for the AtCoder contest