Python Shutil Module Learning Summary

Source: Internet
Author: User
Tags file copy
This article describes the Python shutil module Learning Summary

Shutil name comes from Shell utilities, people who have learned or learned about Linux should be familiar with the shell, you can use this to remember the name of the module. This module has many functions of file (clip) operation, including copying, moving, renaming, deleting and so on.

    1. Chutil.copy (source, destination)
      The Shutil.copy () function implements the file copy function, copying the source file into the destination folder, and two parameters are in string format. If destination is a file name, it is used as the copied file name, which equals copy + rename. Examples are as follows:

      >> Import Shutil
      >> Import OS
      >> os.chdir (' C \ ')
      >> shutil.copy (' C:\spam.txt ', ' C:\delicious ')
      ' C:\delicious\spam.txt '
      >> shutil.copy (' eggs.txt ', ' C:\delicious\eggs2.txt ')
      ' C:\delicious\eggs2.txt '

      As the code shows, the return value of the function is the file path of the string format after the successful copy

    2. Shutil.copytree (source, destination)
      The Shutil.copytree () function copies the entire folder, copying all the contents of the source folder into destination, including the files in source and subfolders that are copied past. All two parameters are in string format.

      Note that if the destination folder already exists, the operation returns a Fileexistserror error that indicates that the file already exists. That is, if the function is executed, the program automatically creates a new folder (the destination parameter) and copies the contents of the source folder to the past
      Examples are as follows:

>> Import Shutil
>> Import OS
>> os.chdir (' C \ ')
>> shutil.copytree (' C:\bacon ', ' C:\bacon_backup ')
\ ' C:\bacon_backup '

As shown in the preceding code, the return value of the function is the absolute path string of the folder after the successful copy so the function can be considered as a backup function
    1. Shutil.move (source, destination)
      The Shutil.move () function moves the source file or folder to destination. The return value is the absolute path string of the file after the move.
      If destination points to a folder, the source file is moved to destination, and the original name is maintained. For example:

>> Import Shutil
>> shutil.move (' C:\bacon.txt ', ' C:\eggs ')
' C:\eggs\bacon.txt '

In the example above, if the file bacon.txt with the same name already exists in the C:\eggs folder, the file will be rewritten by the file with the same name from source. If destination points to a file, the source file is moved and renamed as follows:

>> shutil.move (' C:\bacon.txt ', ' C:\eggs\new_bacon.txt ')
' C:\eggs\new_bacon.txt '

equals move + rename <b> Note that if destination is a folder that does not have a pathname with a suffix, the source will be moved and renamed to Destination</b>, as follows:

>> shutil.move (' C:\bacon.txt ', ' C:\eggs ')
' C:\eggs '

That is, the Bacon.txt file has been renamed to Eggs and is a file without a file suffix. Finally, the destination folder must already exist, otherwise an exception will be thrown:

>> shutil.move (' spam.txt ', ' C:\does_not_exist\eggs\ham ')
Traceback (most recent):
File "D:\Python36\lib\shutil.py", line 538, in move
Os.rename (SRC, REAL_DST)
Filenotfounderror: [Winerror 3] The system cannot find the path specified. : ' Test.txt ', ' C:\does_not_exist\eggs\ham '
During handling of the above exception, another exception occurred:
Traceback (most recent):
File "

    1. Permanently delete files and folders
      There are related functions involved in the OS module
      Os.unlink (path) deletes the path path file
      Os.rmdir (path) will delete the path path folder, but this folder must be empty and not contain any files or subfolders
      Shutil.rmtree (path) deletes the path path folder, and all files and subfolders in this folder will be deleted

Use the function to perform the delete operation, should be cautious, because if you want to delete the TXT file, and accidentally write to the RXT, it will bring trouble to yourself
At this point, we can use the EndsWith property of the string to check and filter the file format.

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.