📜  如何使用Python创建现有文件的重复文件?

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

如何使用Python创建现有文件的重复文件?

在本文中,我们将讨论如何在Python创建现有文件的副本。在目标文件夹中创建重复文件之前,以下是源文件夹和目标文件夹。

在目标文件夹中创建重复文件后,如下图所示。

为了在Python自动复制和删除文件,使用了 shutil 模块。它提供了许多对文件和文件集合的高级操作。使用shutil 模块,我们可以复制文件以及整个目录。



方法一:使用shutil.copyfile()

它以最有效的方式将源文件的内容复制到目标文件。它不使用文件对象,也不复制元数据和权限。

代码:

Python3
# Python program to create the duplicate of
# an already existing file
import os
D = r"F:\Dest"
  
# importing the shutil module
import shutil
  
print("Before copying file:")
print(os.listdir(D))
  
# src contains the path of the source file
src = r"C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
  
# dest contains the path of the destination file
dest = r"F:\Dest\Test.py"
  
# create duplicate of the file at the destination,
# with the name mentioned
# at the end of the destination path
# if a file with the same name doesn't already
# exist at the destination,
# a new file with the name mentioned is created
path = shutil.copyfile(src,dest)
  
print("After copying file:")
print(os.listdir(D))
  
# print path of the newly created duplicate file
print("Path of the duplicate file is:")
print(path)


Python3
# Python program to create the duplicate of
# an already existing file
import os, stat
D = r"F:\Dest"
  
# importing the shutil module
import shutil
  
print("Before copying file:")
print(os.listdir(D))
  
# src contains the path of the source file
src=r"C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
  
# changing the permission(Read, write, and execute 
# by others)
# of the source file
os.chmod(src, stat.S_IRWXO)
  
# dest contains the path of the destination file
dest = r"F:\Dest\Test.py"
  
# create duplicate of the file at the
# destination, with the name mentioned
# at the end of the destination path
# if a file with the same name doesn't
# already exist at the destination,
# a new file with the name mentioned is created
path = shutil.copy(src,dest)
  
# checking the permission of
# the duplicate file to see if the
# permissions have also been copied
# check the permission(Read, write, and execute 
# by others)
# of the duplicate file
print(os.access(path, stat.S_IRWXO))
  
print("After copying file:")
print(os.listdir(D))
  
# print path of the newly created duplicate file
print("Path of the duplicate file is:")
print(path)


Python3
# Python program to create the duplicate of an already 
# existing file
import os
D = r"F:\Dest"
# importing the shutil module
import shutil
  
print("Before copying file:")
print(os.listdir(D))
  
# src contains the path of the source file
src=r"C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
  
# dest contains the path of the destination file
dest = r"F:\Dest\Test.py"
  
# using copy2()
path=shutil.copy2(src,dest)
  
# A new duplicate file is added at
# the destination with name we mention
# on the path
print("After copying file:")
print(os.listdir(D))
  
# print path of the newly created duplicate file
print("Path of the duplicate file is:")
print(path)


输出:

Before copying the file:
['in.txt', 'out.txt']
After copying the file:
['in.txt', 'out.txt', 'Test.py']
Path of the duplicate file is:
F:\Dest\Test.py



方法二:使用shutil.copy()

它还将源文件的内容复制到目标文件或目录。与 copyfile() 不同,shutil.copy() 还复制源文件的权限。

代码:

蟒蛇3

# Python program to create the duplicate of
# an already existing file
import os, stat
D = r"F:\Dest"
  
# importing the shutil module
import shutil
  
print("Before copying file:")
print(os.listdir(D))
  
# src contains the path of the source file
src=r"C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
  
# changing the permission(Read, write, and execute 
# by others)
# of the source file
os.chmod(src, stat.S_IRWXO)
  
# dest contains the path of the destination file
dest = r"F:\Dest\Test.py"
  
# create duplicate of the file at the
# destination, with the name mentioned
# at the end of the destination path
# if a file with the same name doesn't
# already exist at the destination,
# a new file with the name mentioned is created
path = shutil.copy(src,dest)
  
# checking the permission of
# the duplicate file to see if the
# permissions have also been copied
# check the permission(Read, write, and execute 
# by others)
# of the duplicate file
print(os.access(path, stat.S_IRWXO))
  
print("After copying file:")
print(os.listdir(D))
  
# print path of the newly created duplicate file
print("Path of the duplicate file is:")
print(path)

输出:

Before copying the file:
['in.txt', 'out.txt']
After copying the file:
False
['in.txt', 'out.txt', 'Test.py']
Path of the duplicate file is:
F:\Dest\Test.py

方法三:使用shutil.copy2()

它几乎与shutil.copy() 类似,除了copy2() 也尝试保留元数据。当follow_symlinks设置为 False 并且 src 是符号链接时,copy2() 尝试将所有元数据从 src 符号链接复制到新创建的 dst 符号链接。

代码:

蟒蛇3

# Python program to create the duplicate of an already 
# existing file
import os
D = r"F:\Dest"
# importing the shutil module
import shutil
  
print("Before copying file:")
print(os.listdir(D))
  
# src contains the path of the source file
src=r"C:\Users\YASH\OneDrive\Desktop\small\Src\Test.py"
  
# dest contains the path of the destination file
dest = r"F:\Dest\Test.py"
  
# using copy2()
path=shutil.copy2(src,dest)
  
# A new duplicate file is added at
# the destination with name we mention
# on the path
print("After copying file:")
print(os.listdir(D))
  
# print path of the newly created duplicate file
print("Path of the duplicate file is:")
print(path)

输出:

Before copying the file:
['in.txt', 'out.txt']
After copying the file:
['in.txt', 'out.txt', 'Test.py']
Path of the duplicate file is:
F:\Dest\Test.py