📜  Python的Shutil模块

📅  最后修改于: 2022-05-13 01:54:44.958000             🧑  作者: Mango

Python的Shutil模块

Shuutil 模块提供对文件的高级操作,例如对文件的复制、创建和远程操作。它属于 Python 的标准实用程序模块。该模块有助于自动化复制和删除文件和目录的过程。在本文中我们将学习这个模块。

复制文件到另一个目录

Python的shutil.copy()方法用于将源文件的内容复制目标文件或目录。它还保留文件的权限模式,但不保留文件的其他元数据,如文件的创建和修改时间。
必须代表一个文件,但目标可以是文件或目录。如果目标是目录,则文件将使用源中的基本文件名复制到目标中。此外,目的地必须是可写的。如果目标是一个文件并且已经存在,那么它将被文件替换,否则将创建一个新文件。

示例 1:

Python3
# Python program to explain shutil.copy() method 
  
# importing shutil module 
import shutil 
  
source = "path/main.py"
destination ="path/main2.py"
  
# Copy the content of 
# source to destination 
dest = shutil.copy(source, destination) 
  
# Print path of newly 
# created file 
print("Destination path:", dest)


Python3
# importing shutil module  
import shutil 
    
# Source path 
source = "path/main.py"
    
# Destination path 
destination = "path/gfg/"
    
# Copy the content of 
# source to destination 
dest = shutil.copy(source, destination) 
    
  
# Print path of newly  
# created file 
print("Destination path:", dest)


Python3
# Python program to explain shutil.copy2() method 
      
# importing os module 
import os 
  
# importing shutil module 
import shutil 
  
# path 
path = 'csv/'
  
# List files and directories 
# in '/home/User/Documents' 
print("Before copying file:") 
print(os.listdir(path)) 
  
  
# Source path 
source = "csv/main.py"
  
# Print the metadeta 
# of source file 
metadata = os.stat(source) 
print("Metadata:", metadata, "\n") 
  
# Destination path 
destination = "csv/gfg/check.txt"
  
# Copy the content of 
# source to destination 
dest = shutil.copy2(source, destination) 
  
# List files and directories 
# in "/home / User / Documents" 
print("After copying file:") 
print(os.listdir(path)) 
  
# Print the metadata 
# of the destination file 
matadata = os.stat(destination) 
print("Metadata:", metadata) 
  
# Print path of newly 
# created file 
print("Destination path:", dest)


Python3
# Python program to explain shutil.copy2() method 
# importing os module 
import os 
  
# importing shutil module 
import shutil 
  
# Source path 
source = "csv/main.py"
  
# Destination path 
destination = "csv/gfg/"
  
# Copy the content of 
# source to destination 
dest = shutil.copy2(source, destination) 
  
# List files and directories 
# in "/home / User / Desktop" 
print("After copying file:") 
print(os.listdir(destination)) 
  
# Print path of newly 
# created file 
print("Destination path:", dest)


Python3
# Python program to explain shutil.copyfile() method 
# importing shutil module 
import shutil 
  
# Source path 
source = "csv/main.py"
  
# Destination path 
destination = "csv/gfg/main_2.py"
  
dest = shutil.copyfile(source, destination) 
  
print("Destination path:", dest)


Python3
# Python program to explain shutil.copytree() method 
# importing os module 
import os 
  
# importing shutil module 
import shutil 
  
# path 
path = 'C:/Users/ksaty/csv/gfg'
  
print("Before copying file:") 
print(os.listdir(path)) 
  
# Source path 
src = 'C:/Users/ksaty/csv/gfg'
  
# Destination path 
dest = 'C:/Users/ksaty/csv/gfg/dest'
  
# Copy the content of 
# source to destination 
destination = shutil.copytree(src, dest) 
  
print("After copying file:") 
print(os.listdir(path)) 
  
# Print path of newly 
# created file 
print("Destination path:", destination)


Python3
# Python program to demonstrate 
# shutil.rmtree() 
  
import shutil 
import os 
  
# location 
location = "csv/gfg/"
  
# directory 
dir = "dest"
  
# path 
path = os.path.join(location, dir) 
  
# removing directory 
shutil.rmtree(path)


Python3
# importing shutil module  
import shutil  
    
# file search  
cmd = 'anaconda'
    
# Using shutil.which() method 
locate = shutil.which(cmd) 
    
# Print result 
print(locate)


输出:

Destination path: path/main2.py

示例 2:如果目标是目录。

蟒蛇3

# importing shutil module  
import shutil 
    
# Source path 
source = "path/main.py"
    
# Destination path 
destination = "path/gfg/"
    
# Copy the content of 
# source to destination 
dest = shutil.copy(source, destination) 
    
  
# Print path of newly  
# created file 
print("Destination path:", dest) 

输出:

path/gfg/main.py

将元数据与文件一起复制

Python的shutil.copy2()方法用于将源文件的内容复制目标文件或目录。这种方法是相同的shutil.copy()方法,但它也试图保留文件的元数据。

蟒蛇3

# Python program to explain shutil.copy2() method 
      
# importing os module 
import os 
  
# importing shutil module 
import shutil 
  
# path 
path = 'csv/'
  
# List files and directories 
# in '/home/User/Documents' 
print("Before copying file:") 
print(os.listdir(path)) 
  
  
# Source path 
source = "csv/main.py"
  
# Print the metadeta 
# of source file 
metadata = os.stat(source) 
print("Metadata:", metadata, "\n") 
  
# Destination path 
destination = "csv/gfg/check.txt"
  
# Copy the content of 
# source to destination 
dest = shutil.copy2(source, destination) 
  
# List files and directories 
# in "/home / User / Documents" 
print("After copying file:") 
print(os.listdir(path)) 
  
# Print the metadata 
# of the destination file 
matadata = os.stat(destination) 
print("Metadata:", metadata) 
  
# Print path of newly 
# created file 
print("Destination path:", dest) 

输出:

示例 2:如果目标是目录

蟒蛇3

# Python program to explain shutil.copy2() method 
# importing os module 
import os 
  
# importing shutil module 
import shutil 
  
# Source path 
source = "csv/main.py"
  
# Destination path 
destination = "csv/gfg/"
  
# Copy the content of 
# source to destination 
dest = shutil.copy2(source, destination) 
  
# List files and directories 
# in "/home / User / Desktop" 
print("After copying file:") 
print(os.listdir(destination)) 
  
# Print path of newly 
# created file 
print("Destination path:", dest) 

输出:

将一个文件的内容复制到另一个

Python的shutil.copyfile()方法用于将源文件的内容复制到目标文件。不复制文件的元数据。源和目标必须代表一个文件,目标必须是可写的。如果目标已经存在,那么它将被源文件替换,否则将创建一个新文件。

如果源和目标表示同一个文件,则将引发 SameFileError 异常。

蟒蛇3

# Python program to explain shutil.copyfile() method 
# importing shutil module 
import shutil 
  
# Source path 
source = "csv/main.py"
  
# Destination path 
destination = "csv/gfg/main_2.py"
  
dest = shutil.copyfile(source, destination) 
  
print("Destination path:", dest) 

输出:

Destination path: csv/gfg/main_2.py

复制完整目录

shutil.copytree()方法递归地将以源 (src) 为根的整个目录树复制到目标目录。由 (dst) 命名的目标目录必须不存在。它将在复制期间创建。

蟒蛇3

# Python program to explain shutil.copytree() method 
# importing os module 
import os 
  
# importing shutil module 
import shutil 
  
# path 
path = 'C:/Users/ksaty/csv/gfg'
  
print("Before copying file:") 
print(os.listdir(path)) 
  
# Source path 
src = 'C:/Users/ksaty/csv/gfg'
  
# Destination path 
dest = 'C:/Users/ksaty/csv/gfg/dest'
  
# Copy the content of 
# source to destination 
destination = shutil.copytree(src, dest) 
  
print("After copying file:") 
print(os.listdir(path)) 
  
# Print path of newly 
# created file 
print("Destination path:", destination) 

输出:



删除目录

shutil.rmtree()用于删除整个目录树,路径必须指向目录(但不是指向目录的符号链接)。

蟒蛇3

# Python program to demonstrate 
# shutil.rmtree() 
  
import shutil 
import os 
  
# location 
location = "csv/gfg/"
  
# directory 
dir = "dest"
  
# path 
path = os.path.join(location, dir) 
  
# removing directory 
shutil.rmtree(path) 

查找文件

shutil.which()方法告诉可执行应用程序的路径,如果调用给定的cmd ,该应用程序将运行。此方法可用于在 PATH 中存在的计算机上查找文件。

蟒蛇3

# importing shutil module  
import shutil  
    
# file search  
cmd = 'anaconda'
    
# Using shutil.which() method 
locate = shutil.which(cmd) 
    
# Print result 
print(locate)

输出:

D:\Installation_bulk\Scripts\anaconda.EXE