Move, copy, delete, rename files and folders

Source: Internet
Author: User

I. Overview

Python files and folders in the move, copy, delete, rename, mainly rely on the OS module and Shutil module, the following examples in the centralized presentation of the file move, copy, delete, rename, when used to directly query.

Second, examples
1 #!/usr/bin/python32 #-*-coding:utf-8-*-3 __author__='Mayi'4 __date__='2018/4/4'5 6 """7 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #8 Features: file, folder move, copy, delete, rename9 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Ten """ One  A #importing Shutil modules and OS modules - ImportShutil - ImportOS the  - #copying a single file -Shutil.copy ("C:\\a\\1.txt","c:\\b") -  + #Copy and rename a new file -Shutil.copy ("C:\\a\\2.txt","C:\\b\\121.txt") +  A #Copy Entire directory (Backup) atShutil.copytree ("c:\\a","c:\\b\\new_a") -  - #Deleting Files -Os.unlink ("C:\\b\\1.txt") -Os.unlink ("C:\\b\\121.txt") -  in #Delete Empty folders - Try: toOs.rmdir ("c:\\b\\new_a") + exceptException as ex: -     Print("error message:"+str (ex))#Hint: Error message, directory is not empty the  * #recursively delete empty folders $ Try:Panax NotoginsengOs.removedirs ("c:\\b\\new_a") - exceptException as ex: the     Print("error message:"+str (ex))#Hint: Error message, directory is not empty +  A #Delete folders and contents theShutil.rmtree ("c:\\b\\new_a") +  - #Moving Files $Shutil.move ("C:\\a\\1.txt","c:\\b") $  - #move a folder -Shutil.move ("c:\\a\\c","c:\\b") the  - #Renaming FilesWuyiShutil.move ("C:\\a\\2.txt","C:\\a\\new2.txt") the  - #Renaming a folder WuShutil.move ("c:\\a\\d","C:\\a\\new_d") -  About #entry Function $ if __name__=='__main__': -     Pass

Move, copy, delete, rename files and folders

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.