Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python copies folders

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the python how to copy the folder related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python how to copy the folder article will have a harvest, let's take a look.

Specific operation methods:

1. First of all, you need to import os into the python script, and the shutil module performs file operations.

Import os, shutil

2. Use the copy function to copy the folder.

Shutil.copy (source_file,target_ir) # source_file refers to the source path and target_ir refers to the destination path

Example code:

1. Copy the folder as a whole.

Import os

Import shutil

Source_path = os.path.abspath (ringing E:\ Projects\ source_dir')

Target_path = os.path.abspath (ringing E:\ Projects\ new folder\ target_dir')

If not os.path.exists (target_path):

# create if the destination path does not exist in the original folder

Os.makedirs (target_path)

If os.path.exists (source_path):

# delete if the original folder exists in the destination path

Shutil.rmtree (target_path)

Shutil.copytree (source_path, target_path)

Print ('copy dir finishedmakers')

2. Copy all the files under the folder to the destination folder.

Import os

Import shutil

Source_path = os.path.abspath (ringing E:\ Projects\ source_dir')

Target_path = os.path.abspath (ringing E:\ Projects\ target_dir')

If not os.path.exists (target_path):

Os.makedirs (target_path)

If os.path.exists (source_path):

# root refers to the address of the folder that is currently being traversed

# dirs is a list that contains the names of all directories in this folder (excluding subdirectories)

# files is also list and contains all files in this folder (excluding subdirectories)

For root, dirs, files in os.walk (source_path):

For file in files:

Src_file = os.path.join (root, file)

Shutil.copy (src_file, target_path)

Print (src_file)

Print ('copy files finishedmakers')

Related functions:

Os.remove # Delete files

Os.rmdir # Delete folder

Shutil.rmtree # Delete the directory and all its contents

This is the end of the article on "how to copy folders in python". Thank you for reading! I believe you all have a certain understanding of "how to copy folders in python". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report