Get the file path using Pathlib

What I wanted to do

Get a list of files under a specific directory (absolute path format)

It was easy to do with the Pathlib module, which will be available from Python 3 series.

According to the following, pathlib is an object-oriented file path system.

https://docs.python.org/3.4/library/pathlib.html

From the result, I was able to get the list of files in a specific directory by using the following.

from pathlib import Path
from pathlib import PurePath
self.conf_dir =  PurePath('/etc', 'apache', 'conf')
self.conf_files = list(self.conf_dir.glob('*'))

If you print each, the output will be as follows

self.conf_dir = PurePath('/etc', 'apache', 'conf')
print self.conf_dir
output
/etc/apache/conf

Get the obtained path entirely with glob . You can flexibly get the specified file by combining "" with a regular expression etc.

self.conf_files = list(self.conf_dir.glob('*'))
print(self.conf_files)
output
[PosixPath('/etc/apache/conf/http.test1.conf'),PosixPath('/etc/apache/conf/http.test2.conf')]

Recommended Posts

Get the file path using Pathlib
Maya | Get the workspace path
Get the file name in a folder using glob
[Python] Get the official file path of the shortcut file (.lnk)
File renaming using the full path received by the shell
Get the desktop path in Python
Get the script path in Python
Extract the targz file using python
Get the desktop path in Python
[Python] Get the character code of the file
Get the hierarchy name using the OpenMaya iterator
To get the path of the currently running python.exe
Get the update date of the Python memo file.
I want to get the path of the directory where the running file is stored.
Calculation of the shortest path using the Monte Carlo method
Get the GNOME version
Pathlib provides a common interface for file path operations
Get the absolute path of the script you are running
Get the MIME Type
Get the full path referenced by .lnk with wsl
File encryption using OpenSSL
Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory
Get the MIME type in Python and determine the file format
Get the file name saved in AWS S3 (1000 or more)
Paste the image into an excel file using Python's openpyxl
[Python] Get the script execution directory with an absolute path
Get the weather using the API and let the Raspberry Pi speak!
Get similar posts using Doc2Vec
Try using the Twitter API
Clone using the dd command
Get the number of digits
Unzip the internet zip file
File access under the directory
Try using the Twitter API
[Python] Get the previous month
oslo.config config file search path
Get GitHub information using PyGithub
Try using the PeeringDB 2.0 API
File structure when using serverless-python-requirements
Outline the face using Dlib (1)
Can you delete the file?
Find the critical path of PERT using breadth-first search and depth-first search
I tried to get the index of the list using the enumerate function
How to get followers and followers from python using the Mastodon API
[ffmpeg] After ffmpeg using Python subprocess, The system cannot find the path specified.
Read a file in Python with a relative path from the program
Get and set the value of the dropdown menu using Python and Selenium
Get the formula in an excel file as a string in Python
Format the Git log and get the committed file name in csv format