[Work efficiency] How to change file names in Python

Introduction

Do you want to rename a large number of text files and image files at once? This time, I will explain such "how to change the file name at once".

table of contents

  1. [Preparation](# 1-Preparation)
  2. [To rename](# 2-To rename)
  3. [Actually rename files in batch](# 3-Actually rename files in batch)
  4. [Finally](# 4-Finally)

1. Advance preparation

Prepare the file to be renamed. The file can be a text file, an image file, a music file, or a video file. Prepare your favorite file. ↓ ↓ We are preparing an image file this time ↓ ↓ qiita_rename_before.png : warning: Attention: warning: Be sure to back up your data so that you can make mistakes! !! qiita_rename_backup.png

2. To rename

Use the "os.rename ()" function to rename the file. You can rename it by specifying the file before the change in the first argument and the file after the change in the second argument.

python


import os
os.rename(File before change,File after change)

3. Actually change the file name at once

python


# coding: utf-8

import glob
import os

#extension.Get png image file
path = './dir/*.png'
i = 1

#Get an image file
before_file_list = glob.glob(path)
print('Change before')
print(before_file_list)

#Change file names at once
for file in before_file_list:
    os.rename(file, './dir/icon' + str(i) + '.png')
    i += 1

after_file_ist = glob.glob(path)
print('After change')
print(after_file_ist)

↓ ↓ Image file after renaming ↓ ↓ qiita_rename_after.png This sample source is also posted on Github. Github:https://github.com/miyazakikna/RenameFile.git

4. Finally

Here, I explained how to rename files all at once using Python. It is very useful personally because it is simple and you can easily change a large number of file names at once.

Recommended Posts

[Work efficiency] How to change file names in Python
[Python] How to change EXCEL file saved in xlsb to xlsx
How to work with BigQuery in Python
How to create a JSON file in Python
How to change Python version
How to develop in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
[Python] How to convert db file to csv
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to convert Python to an exe file
[Itertools.permutations] How to put permutations in Python
Convert psd file to png in Python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
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
To work with timestamp stations in Python
How to use regular expressions in Python
How to display Hello world in python
How to change editor color in PyCharm
How to use is and == in Python
How to write Ruby to_s in Python
How to use the C library in Python
How to read a CSV file with Python 2/3
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
From file to graph drawing in Python. Elementary elementary
[Python] How to read excel file with pandas
Summary of how to use MNIST in Python
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3
How to read a file in a different directory
How to make Python Interpreter changes in Pycharm