Python batch file suffix modification example code sharing

Source: Internet
Author: User

The goodreader of ipad does not support JS files very well. Although it can be read, it cannot exit and the main interface of goodreader cannot be returned. Therefore, I need to convert js files into plain text in batches. For this purpose, I have implemented the following small application:

Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-

Import OS
Def rename ():
Path = raw_input ("Enter the path of the folder to be processed ")
Print path
Old_ext = "." + raw_input ("Enter the file type to be processed ")
Print old_ext
New_ext = "." + raw_input ("Enter the file type you want to change ")
Print new_ext
For (path, dirs, files) in OS. walk (path): # traverse the directory tree
For filename in files:
Ext = OS. path. splitext (filename) [1] # obtain the file type. Note that it also carries the dot
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 t 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.