Pythonshutil Module Learning Summary

Source: Internet
Author: User
This article introduces Pythonshutil module learning summary This article introduces Python shutil Module Learning Summary

The shutil name comes from shell utilities. anyone who has learned or understood Linux should be familiar with shell and can use it to remember the module name. This module provides many File (folder) operations, including copying, moving, renaming, and deleting.

  1. Chutil. copy (source, destination)
    The shutil. copy () function allows you to copy the source file to the destination folder. Both parameters are in string format. If destination is a file name, it is used as the name of the copied file, that is, copy + rename. Example:

    > 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 shown in the code, the return value of this function is the file path in string format after successful copying.

  2. Shutil. copytree (source, destination)
    The shutil. copytree () function copies the entire folder and copies all the content in the source folder to destination. all files and subfolders in the source folder are copied. Both parameters are in string format.

    Note: If the destination folder already exists, this operation returns a FileExistsError error, prompting that the file already exists. That is, if you execute this function, the program will automatically create a new folder (destination parameter) and copy the content in the source folder.
    Example:

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

As shown in the code above, the return value of this function is the absolute path string of the copied folder. Therefore, this function can be used 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 moved object.
    If destination points to a folder, the source file will be moved to destination and its original name will be kept. For example:

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

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

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

Equal to moving + renamingNote: If destination is a folder, that is, there is no path name with a suffix, the source will be moved and renamed as destination., As follows:

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

That is, the bacon.txt file has been renamed to eggs. it is the end of a file without a file suffix. the destination folder must already exist. Otherwise, an exception is thrown:

> Shutil.move('spam.txt ', 'C: \ does_not_exist \ eggs \ am ')
Traceback (most recent call last ):
File "D: \ Python36 \ lib \ shutil. py", line 538, in move
OS. rename (src, real_dst)
FileNotFoundError: [WinError 3] The system cannot find the specified path. : 'Test.txt '-> 'C: \ does_not_exist \ eggs \ ham'
During handling of the above exception, another exception occurred:
Traceback (most recent call last ):
File"

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

When using the function to perform the delete operation, you should be careful, because if you want to delete the txt file and accidentally write it to rxt, it will bring you trouble
In this case, you can use the endswith attribute of the string to check and filter the file format.

The above is a summary of the Python shutil module. For more information, see other related articles in the first PHP community!

Related Article

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.