Python bulk modified file suffix sample code share

Source: Internet
Author: User
The ipad goodreader to JS file support is not very good, although can read it but always can't quit, back to the GoodReader main interface, so I need to batch JS file into plain text. For this purpose, I have made the following small application:

The code is as follows:


#-*-Coding:utf-8-*-

Import OS
def rename ():
Path = Raw_input ("Enter the folder path to process")
Print path
Old_ext = "." +raw_input ("Please enter the type of file to be processed")
Print Old_ext
New_ext = "." +raw_input ("Please enter the type of file you want to become")
Print New_ext
For (path, dirs, files) in Os.walk (path): #遍历目录树
For filename in Files:
ext = os.path.splitext (filename) [1] #取得文件类型, pay attention to it with the dot number
Print ext;
if (ext = = Old_ext):
Print "----------------"
newname = Filename.replace (Old_ext, New_ext)
OldPath = path + "\ \" + filename
NewPath = path + "\ \" + newname
Try
Os.rename (OldPath, NewPath)
Except Baseexception, E:
Print (str (e))

if __name__ = = ' __main__ ':
Rename ()

  • 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.