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