<Python> A quiz to batch convert file names separated by a specific character string as part of the file name


Someone in Python at https://qiita.com/ozaki25/items/33a57ad7eea55822c764. .. .. Because it was a story I wrote it in Slack for a moment, but I got a story saying "Explanation ~" (probably) and I will write it quickly.

The writing style is my hobby, so there is a better way to write it! Please let me know.

things to do

A brief description of the Python code


ubuntu 18.04 + python 3.7.6.

$ cat /proc/version
Linux version 4.15.0-111-generic (buildd@lcy01-amd64-011) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #112-Ubuntu SMP Thu Jul 9 20:32:34 UTC 2020

$ python3 -V
Python 3.7.6

Posted code

import os
import re
from glob import glob
p_file = glob("./*.csv")
_ = [ os.rename(file, re.sub(r'^.*_([0-9]{1,3}).*(\.csv)$', r'\1\2', file)) for file in p_file ] 

For some reason, the code pasted in Slack was converted to strange code. : nerd: Please see this code correctly.

As a result, this happened because the entry owner insisted on "one liner". .. Of course Python also has a CLI so I can do it, but I don't understand why so I stopped here.

Is data creation one-liner? So it seems a little foul.

!touch aaa_1.csv;touch aaa_2.csv;touch aaa_4.csv;touch aaa_9.csv;touch aaa_10.csv;touch aaa_99.csv;touch aaa_100.csv;touch aaa_999.csv;touch aaa_1000.csv

Now for the explanation.

import os
import re
from glob import glob

File rename is os.rename Regular expression re for file name replacement I used.

I used glob to get the file list object. I've been using similar pathlibs these days, I don't use join anymore.

p_file = glob("./*.csv")

Use glob to get the file list object. I didn't want to target extra files, but glob doesn't support multiple matches, so for the time being, I'm not good at fetching all the csv extensions. ... I was a little addicted to it.

_ = [ os.rename(file, re.sub(r'^.*_([0-9]{1,3}).*(\.csv)$', r'\1\2', file)) for file in p_file ] 

Yes, I don't know unless I'm used to it. I used comprehensions to reduce the number of lines (although there are different benefits).

It looks like this if you write it without using intensions.

for file in p_file:
  re_file = re.sub(r'^.*_([0-9]{1,3}),*(\.csv)$', r'\1\2',p_file)
  os.rename(file, re_file)

It doesn't make much sense to assign the result to _, and if you don't specify it, the console log isn't beautiful, so it's a painstaking measure to prevent it from appearing.

[None, None, None, None, None, None, None, None, None, None]

what is it. .. .. this.

The last is a regular expression The part containing 1 to 3 digits and the extension part are extracted and assembled (standard match + replacement). It may be a little sweet to narrow down by regular expressions.

r'^.*_([0-9]{1,3}).*(\.csv)$' ⇛ r'\1\2'

It is recommended to use r (row string expression) because it can be expressed without extra escaping.


I'm worried about the read permission of the file or something, but I'm omitting it this time. However, when operating unattended, it is better to include basic existence check / read / write authority.

It may fail with os.rename, so you should also try ~ except. This time, I skipped it because I focused on quickness (excuse: umbrella2 :)

In the code used in the company, the CSV file is read and imported into the DataFrame, but a Class that wraps the existence check and read / write check is created and operated.

At the end

I have recently been transferred, and as I can see the whole picture of the project I am in charge of, I feel pressured. .. .. I'm relieved to write Python code (just kidding: sweat_smile :).

Recommended Posts

<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
Get the variable name of the variable as a character string.
I want to batch convert the result of "string" .split () in Python
Convert the character code of the file with Python3
[Python] I tried to get the type name as a string from the type function
Cut a part of the string using a Python slice
[Ansible] Example of playbook that adds a character string to the first line of the file
[Ruby] How to replace only a part of the string matched by the regular expression?
[Python3] Format the character string using the variable name as the key.
[Python] How to make a list of character strings character by character
[python] Change the image file name to a serial number
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to quickly count the frequency of appearance of characters from a character string in Python?
pandas Fetch the name of a column that contains a specific character
[Introduction to Python] How to split a character string with the split function
Python vba to create a date string for creating a file name
Get the formula in an excel file as a string in Python
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
A memo organized by renaming the file names in the folder with python
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
[Python] How to invert a character string
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
I want to color a part of an Excel string in Python
[Introduction to Python] How to write a character string with the format function
[Python] Get the character code of the file
[Introduction to Python] Thorough explanation of the character string type used in Python!
I made a program to check the size of a file in Python
The story of making a tool to load an image with Python ⇒ save it as another name
How to input a character string in Python and output it as it is or in the opposite direction.
Various ways to read the last line of a csv file in Python
Divides the character string by the specified number of characters. In Ruby and Python.
Note) Batch conversion of specific symbols contained in a character string with a dictionary
[python] How to sort by the Nth Mth element of a multidimensional array
Python tricks: a combination of enumerate () and zip (), checking if a string can be converted to a number, sorting the string as a number
[Python] Save the video data imported by OpenCV as a serial number jpg file
[Python] You can save an object to a file by using the pickle module.
[Python] Leave only the elements that start with a specific character string in the array
Notification of weather forecast (rain, etc.) by DM as a part of the function of bot
Don't take an instance of a Python exception class directly as an argument to the exception class!
Add a function to tell the weather of today to slack bot (made by python)
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
# Function that returns the character code of a string
Read the xml file by referring to the Python tutorial
Output the output result of sklearn.metrics.classification_report as a CSV file
Python Note: The mystery of assigning a variable to a variable
I tried to summarize the string operations of Python
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
Spit out a list of file name, last modified date and character code in python3
Get the value of a specific key up to the specified index in the dictionary list in Python
What seems to be a template of the standard input part of the competition pro in python3
Python> __init__.py> Required to handle the specified directory as a package (empty file is acceptable)
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
Find out the apparent width of a string in python
How to switch the configuration file to be read by Python
How to shuffle a part of a Python list (at random.shuffle)
Change the standard output destination to a file in Python
Get the number of specific elements in a python list