📜  Python| shutil.copyfile() 方法

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

Python| shutil.copyfile() 方法

Python3
# Python program to explain shutil.copyfile() method
   
# importing shutil module
import shutil
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents"
 
# Copy the content of
# source to destination
 
try:
    shutil.copyfile(source, destination)
    print("File copied successfully.")
 
# If source and destination are same
except shutil.SameFileError:
    print("Source and destination represents the same file.")
 
# If destination is a directory.
except IsADirectoryError:
    print("Destination is a directory.")
 
# If there is any permission issue
except PermissionError:
    print("Permission denied.")
 
# For other errors
except:
    print("Error occurred while copying file.")


Python3
# Python program to explain shutil.copyfile() method
   
# importing os module
import os
 
# importing shutil module
import shutil
 
# path
path = '/home/User/Documents'
 
# List files and directories
# in '/home/User/Documents'
print("Before copying file:")
print(os.listdir(path))
 
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents/file(copy).txt"
 
# Copy the content of
# source to destination
dest = shutil.copyfile(source, destination)
 
# List files and directories
# in "/home / User / Documents"
print("After copying file:")
print(os.listdir(path))
 
# Print path of newly
# created file
print("Destination path:", dest)


Python3
# Python program to explain shutil.copyfile() method
   
# importing shutil module
import shutil
 
 
# If the source and destination
# represents the same file
# 'SameFileError' exception
# will be raised
 
# If the destination is
# is directory then
# 'IsADirectoryError' exception
# will be raised
 
# If the destination is
# not writable
# 'PermissionError' exception
# will be raised
 
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents/file.txt"
 
# Copy the content of
# source to destination
shutil.copyfile(source, destination)


Python3
# Python program to explain shutil.copyfile() method
   
# importing shutil module
import shutil
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents"
 
# Copy the content of
# source to destination
 
try:
    shutil.copyfile(source, destination)
    print("File copied successfully.")
 
# If source and destination are same
except shutil.SameFileError:
    print("Source and destination represents the same file.")
 
# If destination is a directory.
except IsADirectoryError:
    print("Destination is a directory.")
 
# If there is any permission issue
except PermissionError:
    print("Permission denied.")
 
# For other errors
except:
    print("Error occurred while copying file.")


Python中的Shutil 模块提供了许多对文件和文件集合进行高级操作的功能。它属于 Python 的标准实用程序模块。该模块有助于自动复制和删除文件和目录的过程。
Python中的shutil.copyfile()方法用于将文件的内容复制到目标文件。不复制文件的元数据。目标必须代表一个文件,而目标必须是可写的。如果目标已经存在,那么它将被文件替换,否则将创建一个新文件。
如果目标代表同一个文件,则会引发 SameFileError异常。

代码 #1:使用 shutil.copyfile() 方法将文件从源复制到目标

Python3

# Python program to explain shutil.copyfile() method
   
# importing os module
import os
 
# importing shutil module
import shutil
 
# path
path = '/home/User/Documents'
 
# List files and directories
# in '/home/User/Documents'
print("Before copying file:")
print(os.listdir(path))
 
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents/file(copy).txt"
 
# Copy the content of
# source to destination
dest = shutil.copyfile(source, destination)
 
# List files and directories
# in "/home / User / Documents"
print("After copying file:")
print(os.listdir(path))
 
# Print path of newly
# created file
print("Destination path:", dest)
输出:
Before copying file:
['hrithik.png', 'test.py', 'sample.txt', 'file.text', 'copy.cpp']
After copying file:
['hrithik.png', 'test.py', 'sample.txt', 'file.text', 'file(copy).txt', 'copy.cpp']
Destination path: /home/User/Documents/file(copy).txt

代码 #2:使用 shutil.copyfile() 方法时可能出现的错误

Python3

# Python program to explain shutil.copyfile() method
   
# importing shutil module
import shutil
 
 
# If the source and destination
# represents the same file
# 'SameFileError' exception
# will be raised
 
# If the destination is
# is directory then
# 'IsADirectoryError' exception
# will be raised
 
# If the destination is
# not writable
# 'PermissionError' exception
# will be raised
 
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents/file.txt"
 
# Copy the content of
# source to destination
shutil.copyfile(source, destination)
输出:
Traceback (most recent call last):
  File "copy.py", line 31, in 
    shutil.copyfile(source, destination)
  File "/usr/lib/python3.6/shutil.py", line 104, in copyfile
    raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: '/home/User/Documents/file.txt' and '/home/User/Documents/file.txt'
are the same file

代码 #3:使用 shutil.copyfile() 方法时处理错误

Python3

# Python program to explain shutil.copyfile() method
   
# importing shutil module
import shutil
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents"
 
# Copy the content of
# source to destination
 
try:
    shutil.copyfile(source, destination)
    print("File copied successfully.")
 
# If source and destination are same
except shutil.SameFileError:
    print("Source and destination represents the same file.")
 
# If destination is a directory.
except IsADirectoryError:
    print("Destination is a directory.")
 
# If there is any permission issue
except PermissionError:
    print("Permission denied.")
 
# For other errors
except:
    print("Error occurred while copying file.")
输出:
Destination is a directory.

参考: https://docs。 Python.org/3/library/shutil.html