Python File Rename Tool code _python

Source: Internet
Author: User
Copy Code code as follows:

#Filename: brn.py
#Description: Batch Replace certain words in file names
#Use to bat Rename the "file in a dir" (Modify the suffix from "A to B") for Windows Vista OS
Import Sys
Import OS
Import Fnmatch
Import re
#parse params
P=input ("Please input work directory (current path for Enter):")
If p== ' \ R ':
P= '. '
P=p.rstrip (' \ R ')
Print (P)
While not os.path.exists (p):
Print (p+ ' is not existed. Please input the work directory: ')
P=input ("Please input work directory (current path for Enter):")
S=input ("Please enter the words which need to be modified (must):")
While s== ' \ R ':
S=input ("Please enter the words which need to be replaced (must):")
S=s.rstrip (' \ R ')
D=input ("Please enter the words which want to" (MUST): ")
While d== ' \ R ':
D=input ("Please enter the words which want to" (MUST): ")
D=d.rstrip (' \ R ')
Try
Sure=input ("Are you sure to rename the file named *" +s+ "*" + "to *" +d+ "*" + "in Directory" +p+ "? y/n:")
Sure=sure.rstrip (' \ R ')
If sure!= ' y ':
Print ("Cancel")
Else
For Root, dirs, the files in Os.walk (P, True):
For file in Files:
Print (Os.path.join (root,file))
If Os.path.isfile (Os.path.join (root,file)): #Only file is File,not a dir, does this
If Fnmatch.fnmatch (file, ' * ' +s+ ' * '):
F=str (file). Replace (S,D)
If p== '. ':
command= ' Move ' +str (file) + "" +f
Else
Command= "Move" +os.path.join (root,file) + "" +os.path.join (ROOT,F)
Print (command)
If Os.system (command) ==0: #do actual rename
Print ("Rename" +str (file) + "to" +f+ "success")
Else
Print ("Rename" +str (file) + "to" +f+ "failed")
#else:
#print Str (file) + "is a Directory.omit"
Except Indexerror:
Print (Indexerror.message)
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.