20230809 Use python3 to batch convert TXT files to SRT files under WIN10

20230809 Use python3 to batch convert TXT files to SRT files under WIN102023
/8/9 17:30


Since I like to watch documentaries and other foreign videos, after identifying the subtitles through clipping/PR2023/AUTOSUB, I can use Google Translate to identify them as Simplified Chinese DOCX documents.
After the DOCX document is converted to a TXT document, it also needs to be converted to an SRT document.
This article only introduces how to directly change the extension, but does not modify the TXT content.

Of course:
1. The DOCX/SRT subtitles of Google Translate need to be modified. [Google Translate has substituted the serial number into the translation! ^_]
2. You can only convert DOCX documents into TXT documents using office/WPS, and then manually modify the extension! The python extension library can directly obtain SRT subtitles (no TXT required for transfer!)


python batch rename

 

 


https://pythonjishu.com/nwbuyryewwscpxl/How
to use Python to batch rename files
June 26, 2023 11:35 pm ? other


When we need to rename a large number of files in batches, manually modifying them one by one is obviously too inefficient. Below we will introduce how to use Python to batch rename files.

Prerequisite knowledge
Before learning Python to rename files in batches, you need to master the following knowledge:

Learn to use Python's os module for file operations.
Learn to use Python's shutil module for directory operations.
Learn to use Python's regular expression re module.
Basic process
. Use the os module to open directories that need to be renamed in batches, and traverse files.
For each file, use the os module. Get the file name and directory name, and construct a new file name.
For the construction of the file name, we can use some basic operations of strings, such as slicing, splicing, etc., or we can use regular expressions.
Use the os module to rename files.
Code implementation
. Two examples are given below:

Example 1: Modify file suffixes in batches.
Modify all txt files in the current directory into json files.

import us

# Get the current directory
path = os.getcwd()
# View all files in the current directory files
= os.listdir(path)

# Traverse all files
for file in files:
    # Determine whether the file is a txt file
    if file.endswith('.txt'):
        # Construct a new file name
        new_file = file.replace('.txt', '.json')
        # Rename files
        os.rename(os.path.join(path, file), os.path.join(path, new_file))
Example 2: Batch modify the numbers in the file names
to remove the numbers from all file names in the current directory .

Popular recommendations from the Python Technology Station:
PDF electronic invoice recognition software, identify electronic invoices with one click and import them into Excel!
10 top data mining software!
The top ten functions of artificial intelligence!
import os
import re

# Get the current directory
path = os.getcwd()
# View all files in the current directory files
= os.listdir(path)

# Traverse all files
for file in files:
    # Determine whether the file name contains numbers
    if re.search('\d+', file):
        # Construct a new file name and replace the numbers with empty strings
        new_file = re.sub( '\d+', '', file)
        # Rename the file
        os.rename(os.path.join(path, file), os.path.join(path, new_file))
In summary
, the above is to use Python to batch rename files. method, it should be noted that it is best to back up the original file before operation to avoid file loss caused by misoperation. By learning the above basic process and sample code, you can flexibly use Python to batch rename files according to your own needs.

 

 


LOG:
J:\! ! ! ! Documentation 20230625\en2cn\20230809 Use python3 to batch convert TXT files to SRT files under WIN10\ansi>The
 volume in DIR drive J is 18680688682.
 The serial number of the volume is 2A59-69C0

 J:\! ! ! ! Documentation 20230625\en2cn\20230809 Use python3 to batch convert TXT files to SRT files under WIN10\ansi directory

2023/08/09 17:51 <DIR> .
2023/08/09 17:51 <DIR> ..
2023/08/09 12:22 67,713 August 7.txt
2023/08/09 12:22 113,997 AC3EN2 .silhouette.txt
2023/08/09 12:22 67,713 path_to_your_word_file.txt 2023/08/09
12:22 75,347 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.eng9. txt
2023/08/09 17:51 <DIR> utf8
               4 files 324,770 bytes
               3 directories 50,788,548,608 available bytes

J:\! ! ! ! Documentation 20230625\en2cn\20230809 Use python3 to batch convert TXT files to SRT files under WIN10\ansi>python
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v .1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information. >>>
>>>
import os
>>>
>>> # Get the current directory
>> > path = os.getcwd()
>>> # View all files in the current directory
>>> files = os.listdir(path)
>>>
>>> # Traverse all files
>>> for file in files:
... # Determine whether the file is a txt file
... if file.endswith('.txt'):
... # Construct a new file name
... new_file = file.replace('.txt',

...         os.rename(os.path.join(path, file), os.path.join(path, new_file))
...
>>> exit()

J:\! ! ! ! Documentation 20230625\en2cn\20230809 Use python3 to batch convert TXT files to SRT files under WIN10\ansi>
J:\! ! ! ! Documentation 20230625\en2cn\20230809 Use python3 to batch convert TXT files to SRT files under WIN10\ansi>dir The
 volume in drive J is 18680688682
 The serial number of the volume is 2A59-69C0

 J:\! ! ! ! Documentation 20230625\en2cn\20230809 Use python3 to batch convert TXT files to SRT files under WIN10\ansi directory

2023/08/09 17:52 <DIR> .
2023/08/09 17:52 <DIR> ..
2023/08/09 12:22 67,713 August 7.json
2023/08/09 12:22 113,997 AC3EN2 .silhouette.json
2023/08/09 12:22 67,713 path_to_your_word_file.json 2023/08/09
12:22 75,347 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.eng9. json
2023/08/09 17:51 <DIR> utf8
               4 files 324,770 bytes
               3 directories 50,788,548,608 available bytes

J:\! ! ! ! Documentation 20230625\en2cn\20230809 Use python3 to batch convert TXT files to SRT files under WIN10\ansi>

 


txt2json.py

# coding=utf-8
import os

# Get the current directory
path = os.getcwd()
# View all files in the current directory files
= os.listdir(path)

# Traverse all files
for file in files:
    # Determine whether the file is a txt file
    if file.endswith('.txt'):
        # Construct a new file name
        new_file = file.replace('.txt', '.json')
        # Rename the file
        os.rename(os.path.join(path, file), os.path.join(path, new_file))


Reference:
https://pythonjishu.com/nwbuyryewwscpxl/How
to batch rename files using Python


https://www.1mxy.cn/8050.htmlHow
to use Python to batch rename files


https://blog.csdn.net/m0_61789994/article/details/130901574
Use python to batch rename files in a folder


https://blog.51cto.com/u_16175450/6798626
python write file and press Enter


 

Guess you like

Origin blog.csdn.net/wb4916/article/details/132194024