Python Mini Exercise 02 Batch modification of file names

Source: Internet
Author: User

Learning to modify the file name when the thought of their own download of the novel name is a bit long, on the phone to see only the century before the hundred strong ... It's not very comfortable to look at, so I want to apply the name to shorten it. Here's how the diagram looks like before it changes:

1 ImportOs,sys2Path ='E:\MyPython\ century Hundred strong'3dirs = Os.listdir (path)#list directories under Path4  forFileinchdirs:5Name = File.replace (' ',"')#Remove Spaces6NewName = name[7:]#Remove the preceding word7Os.rename (Os.path.join (Path,file), Os.path.join (Path,newname))#to go to the same directory as the file before renaming

Effect:

Python Mini Exercise 02 Batch modification of file names

Related Article

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.