Python batch file suffix modification method

Source: Internet
Author: User
This article mainly introduces how to modify the file suffix in Python in batches. if you need it, you can refer to the many tutorials you have downloaded recently. Unfortunately, the suffix names are all ". mp4 ", and I like ". rmvb "suffix, due to slight cleanliness, cannot stand the following ". mp4 ", but manual modification is too cumbersome, so use the Python you just learned to be lazy! :)

Name of the file before running the program

What we need to do is to create a python file in the current directory, such as demo2.py, and then open it in the editor and type the following code:

The code is as follows:


Import OS

# List all files in the current directory
Files = OS. listdir (".")

For filename in files:
Portion = OS. path. splitext (filename)
# If the suffix is .txt
If portion [1] = ". mp4 ":
# Recombine the file name and suffix
Newname = portion [0] + ". rmvb"
OS. rename (filename, newname)

Okay. save and run your program!

You will be surprised to find that:

Haha, all files suffixed with ". mp4" should be suffixed with ". rmvb !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.