How to replace the file name and content in a folder with python

Source: Internet
Author: User
This article introduces how to replace the file name and file content in a folder with python. The example shows the effect:

1. replace the name of the folder and subfolders in a folder from OldStrDir to NewStrDir;

2. replace the names of folders in a folder and all files in subfolders with OldStrFile to NewStrFile;

3. replace the contents of folders in a folder and all files in subfolders with OldStrContent changed to NewStrContent;

Code:

# -*- coding: UTF-8 -*-import osimport re#replace dir namedef replaceDirName(rootDir,oldStr,newStr):    for parent,dirNames,fileNames in os.walk(rootDir,topdown=False):        for dirName in dirNames:            if oldStr in dirName:                dirNameOld = os.path.join(parent,dirName)                dirNameNew = os.path.join(parent,dirName.replace(oldStr,newStr))                print(dirNameOld  + ' --> ' +  dirNameNew)                os.rename(dirNameOld,dirNameNew)                        #replace file namedef replaceFileName(rootDir,oldStr,newStr):    for parent,dirNames,fileNames in os.walk(rootDir):        for fileName in fileNames:            if oldStr in fileName:                fileNameOld = os.path.join(parent,fileName)                fileNameNew =  os.path.join(parent,fileName.replace(oldStr,newStr))                print(fileNameOld  + ' --> ' +  fileNameNew)                os.rename(fileNameOld,fileNameNew)            #replace file content namedef replaceFileContent(rootDir,oldStr,newStr):    for parent,dirNames,fileNames in os.walk(rootDir):        for fileName in fileNames:            fileObj = os.path.join(parent,fileName)            f = open(fileObj,'r+')            all_the_lines=f.readlines()            f.seek(0)            f.truncate()            for line in all_the_lines:                f.write(line.replace(oldStr,newStr))                f.close()  def main():    rootDir = "D:/D"    oldStr = "CustomerType"    newStr = "CustomerAttr"    replaceDirName(rootDir,oldStr,newStr)    replaceFileName(rootDir,oldStr,newStr)    replaceFileContent(rootDir,oldStr,newStr)if __name__=='__main__':    main()

The above describes how to replace the file name and file content in the folder with python. For more information, see other related articles in the first PHP community!

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.