Table of Contents
Get the file list
Filter video files
Batch rename
Home Backend Development Python Tutorial Python magic operation! Out-of-order file renaming numbers!

Python magic operation! Out-of-order file renaming numbers!

Apr 12, 2023 pm 04:19 PM
python document Order

Python magic operation! Out-of-order file renaming numbers!

As shown in the figure below, there is a bunch of video files in the local folder. In this case, they are not out of order.

Python magic operation! Out-of-order file renaming numbers!

#But after uploading it to the network disk, it will often become out of order. That is, they will be sorted according to 1, 10, 11, 2, 20, which is not convenient for us to watch them in order.

Python magic operation! Out-of-order file renaming numbers!

So we hope to be able to rename them locally. For example, we can sort them according to 001, 002, 003···, 020, so as to avoid the above embarrassment. situation.

In Python, the os module can be used to automatically handle various files and directories, such as copying, moving, renaming, and deleting operations.

Get the file list

Enter the following command in the interactive environment:

import os
path =os.getcwd()
filenames = os.listdir(path)
filenames
Copy after login

Output:

Python magic operation! Out-of-order file renaming numbers!

os module The getcwd() function in , you can use it to get the current working directory. The listdir() function in the os module can return all files and subdirectories in the working directory. Through these two functions, we obtain all files in the current working directory.

Filter video files

Enter the following command in the interactive environment:

file_mp4s = [i for i in filenames if i.split(".")[-1] == "mp4"]
file_mp4s
Copy after login

Output:

Python magic operation! Out-of-order file renaming numbers!

This Step is used to filter all mp4 files in the file list. Using loop conditions is too cumbersome, but list generation can get the video file with one line of statements.

Batch rename

Enter the following command in the interactive environment:

for i in file_mp4s:
 new_name = i.split("-")[0].zfill(3) + "-" + i.split("-")[1]
 os.rename(i,new_name)
Copy after login

Output:

Python magic operation! Out-of-order file renaming numbers!

os module The rename() function in , you can use it to rename files.

A string function zfill() is also used here, which will return a string of specified length. The original string is right-aligned and filled with 0s in front. So "1".zfill(3) will return '001'.

In this way, we have achieved the renaming and numbering of out-of-order files. I hope today’s sharing can be helpful to you~

The above is the detailed content of Python magic operation! Out-of-order file renaming numbers!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

How to search deepseek

What software is NET40? What software is NET40? May 10, 2024 am 01:12 AM

What software is NET40?

See all articles