See file and folder information on python

Summary related to python file operations that I often use but check every time

Get a list of files in a folder

Get a list of files in the specified folder as a list

import glob

folder = "./tmp/" 
img_path_list = glob.glob(folder)

You can also get a list of specific files by using wildcards The following gets only png files as a list

img_path_list = glob.glob(folder + "*.png ")

Get file name

Get the file name as a character string from the specified path

import os

filepath = './tmp/hogehoge.ext'

filename = os.path.basename(filepath)
print(filename) # hogehoge.ext

When acquiring without extension

filename_only = os.path.splitext(os.path.basename(filepath))[0]
print(filename_only) # hogehoge 

Create folder if it does not exist

Check if the save destination folder exists when saving the file, etc. If it does not exist, create a new one

import os

filepath = "./hogehoge2/" 
if not os.path.exists(filepath):
        os.makedirs(filepath)

Recommended Posts

See file and folder information on python
Python memo ① Folder and file operations
Python on Ruby and angry Ruby on Python
Notes on Python and dictionary types
Python 3.6 on Windows ... and to Xamarin.
Tips on Python file input / output
Integrate Modelica and Python on Windows
Python CSV file reading and writing
Python CGI file created on Windows
Get delay information on Twitter and tweet
Python and Bash on Cisco Catalyst IOS-XE
Build Python3 and OpenCV environment on Ubuntu 18.04
Python virtual environment and packages on Ubuntu
A memo with Python2.7 and Python3 on CentOS
Copy file and rewrite cell value @python
Write O_SYNC file in C and Python
Notes on building Python and pyenv on Mac
Understand python lists, dictionaries, and so on.
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
[python] Read html file and practice scraping
[Python] Reading and writing photo location information tags (JPG file GPS Exif)
Create and edit spreadsheets in any folder on Google Drive with python
Read the file with python and delete the line breaks [Notes on reading the file]
Script python file
Install and run Python3.5 + NumPy + SciPy on Windows 10
File DL, byte value and delete in Python3
Python on Windows
twitter on python3
Notes on installing Python3 and using pip on Windows7
Python file processing
Output python log to both console and file
python on mac
PHP and Python integration from scratch on Laravel
Python on Windbg
[Note] Installing Python 3.6 + α on Windows and RHEL
Try importing MLB data on Mac and Python
Install MongoDB on Ubuntu 16.04 and operate via python
Install Python and libraries for Python on MacOS Catalina
Install ZIP version Python and pip on Windows 10
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
Initial settings for using Python3.8 and pip on CentOS8
Install Python3 on Mac and build environment [Definitive Edition]
Notes on HDR and RAW image processing with Python
Install selenium on Mac and try it with python
Automatic follow on Twitter with python and selenium! (RPA)
Information for controlling the motor with Python on RaspberryPi
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
Installing Python 3 on Mac and checking basic operation Part 1
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Read json file with Python, format it, and output json
Compile and install MySQL-python for python2.7 on amazon linux
Email hipchat with postfix, fluentd and python on Azure
Automate Chrome with Python and Selenium on your Chromebook
Install pyenv on Raspberry Pi and version control Python
[Python] Get user information and article information with Qiita API
Create a decent shell and python environment on Windows
Folder creation / file move / compress / delete operations with python
Install django on python + anaconda and start the server
[Python] Start a batch file from Python and pass variables.
Set up python and machine learning libraries on Ubuntu
Use Python to monitor Windows and Mac and collect information on the apps you are working on