Python shutil learning notes, pythonshutil

Source: Internet
Author: User

Python shutil learning notes, pythonshutil

Introduction

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)

shutil.copy()The function copies 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.

Ii. shutil. copytree (source, destination)

shutil.copytree()The function copies the entire folder and copies all the content in the source folder to destination, including the files and subfolders in the source folder. 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 folder after the copy is successful.

Therefore, this function can be used as a backup function.

3. shutil. move (source, destination)

shutil.move()The 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 + renaming

<B> Note: If destination is a folder without a suffix, the source will be moved and renamed as destination. </B>:

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

That is, the bacon.txt file has been renamed to eggs. It is a file without a file suffix.

Finally, the destination folder must already exist; otherwise, an exception is thrown:

> Shutil.move('spam.txt ', 'c: \ does_not_exist \ eggs \ ham') 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"

Iv. Permanently delete files and folders

Related functions in the OS module are involved here.

os.unlink(path)Will delete the path File

os.rmdir(path)Will delete the path folder, but this folder must be empty, does not contain any files or subfolders

shutil.rmtree(path)Will delete the path folder, and all files and subfolders in this folder will be 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.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.