📌  相关文章
📜  Python - 将文件从子文件夹复制到主文件夹

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

Python - 将文件从子文件夹复制到主文件夹

在本文中,我们将讨论如何将文件从子文件夹复制到主文件夹。本文将用于说明的目录树如下所示:

D:\projects\base
|
|__subfolder:
|    \__file.txt
|      
|__main.py
| 

这里我们有一个名为“base”的文件夹,其中有一个名为“subfolder”的文件夹,其中包含文件名 file.txt。我们将从子文件夹中复制基本文件夹中的 file.txt。

Python附带了各种模块,提供了多种执行输入/输出操作的方法,我们将在此处讨论它们。

我们将讨论的各种方法包括:-



  1. 使用shutil.copyfile()
  2. 使用shutil.copy()
  3. 使用shutil.copy2()
  4. 使用shutil.copyfileobj()

方法一:使用shutil.copyfile()

使用shutil 库的copyfile() 方法,我们可以轻松地将文件从一个位置复制到另一个位置。它需要 2 个参数,即需要复制的文件所在的源路径和需要复制的文件的目标路径。下面是这个方法的实现代码:

Python
# importing the shutil module
import shutil
  
# storing the current path of file.txt
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination path in the
# destination variable
destination = 'D:/projects/base/file.txt'
  
# calling the shutil.copyfile() method
shutil.copyfile(source,destination)


Python
# importing the shutil module
import shutil
  
# storing the current file path of file.txt
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination directory in the
# destination variable
destination = 'D:/projects/base'
  
# calling the shutil.copy() method
shutil.copy(source,destination)


Python
# importing the shutil module
import shutil
  
# storing the current path of file.txt 
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination path in the
# destination variable
destination = 'D:/projects/base'
  
# calling the shutil.copy2 method
shutil.copy2(source,destination)


Python
# importing shutil module 
import shutil
    
# Source file
source = 'D:/projects/base/subfolder/file.txt'
    
# Open the source file
# in read mode and
# get the file object
fsrc = open(source, 'r') 
    
    
# destination file
dest = 'D:/projects/base/file.txt'
    
# Open the destination file
# in write mode and
# get the file object
fdst = open(dest, 'w')
    
    
# Now, copy the contents of
# file object f1 to f2 
# using shutil.copyfileobj() method
shutil.copyfileobj(fsrc, fdst)
    
# We can also specify
# the buffer size by paasing
# optional length parameter
# like shutil.copyfileobj(fsrc, fdst, 1024)
  
    
# Close file objects
fdst.close()
fsrc.close()


运行此代码后,我们会注意到名为 file.txt 的文件已复制到基本文件夹。

关于shutil.copyfile() 方法的一些注意事项:-

  • 目标位置必须是可写的,否则将引发 IOError 异常。
  • 如果目的地已经存在,它将被替换。
  • 使用此函数无法复制特殊文件,例如字符或块设备和管道。
  • 它不复制元数据。

方法二:使用shutil.copy()

使用shutil.copy() 方法我们也可以复制文件,我们可以说的唯一语法变化是我们传递的目标字符串也可以是目录而不是完整的文件路径。例如看下面的代码:

Python

# importing the shutil module
import shutil
  
# storing the current file path of file.txt
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination directory in the
# destination variable
destination = 'D:/projects/base'
  
# calling the shutil.copy() method
shutil.copy(source,destination)

运行此代码后,我们会注意到名为 file.txt 的文件已复制到基本文件夹。



copyfile() 和 copy() 方法的主要区别是:

  • copy() 方法还复制有关权限的信息,但 copyfile() 方法不会。
  • 在 copy() 方法中,目标字符串可以是目录或完整文件路径,但在 copyfile() 方法中,它应该是完整文件路径。

方法三:使用shutil.copy2()

Shutil.copy2() 方法也类似于 shutil.copy() 方法,但它做的额外事情是复制元数据,如时间戳。
代码将类似于上面:

Python

# importing the shutil module
import shutil
  
# storing the current path of file.txt 
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination path in the
# destination variable
destination = 'D:/projects/base'
  
# calling the shutil.copy2 method
shutil.copy2(source,destination)

运行此代码后,我们会注意到名为 file.txt 的文件已复制到基本文件夹。

方法四:使用shutil.copyfileobj()

Shutil.copyfileobj() 也类似于上面讨论的方法,但不同的是首先它需要文件对象而不是文件路径,其次它需要一个额外的选项参数,即在复制过程中保留在内存中的字节数。默认值为 16KB。

使用上述方法复制的代码将是:

Python

# importing shutil module 
import shutil
    
# Source file
source = 'D:/projects/base/subfolder/file.txt'
    
# Open the source file
# in read mode and
# get the file object
fsrc = open(source, 'r') 
    
    
# destination file
dest = 'D:/projects/base/file.txt'
    
# Open the destination file
# in write mode and
# get the file object
fdst = open(dest, 'w')
    
    
# Now, copy the contents of
# file object f1 to f2 
# using shutil.copyfileobj() method
shutil.copyfileobj(fsrc, fdst)
    
# We can also specify
# the buffer size by paasing
# optional length parameter
# like shutil.copyfileobj(fsrc, fdst, 1024)
  
    
# Close file objects
fdst.close()
fsrc.close()

运行此代码后,我们会注意到名为 file.txt 的文件已复制到基本文件夹。

我们还可以使用给定的表格记住上述方法之间的区别: