📌  相关文章
📜  Python – 移动和覆盖文件和文件夹

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

Python – 移动和覆盖文件和文件夹

在本文中,我们将学习如何移动文件和文件夹的集合,其中可能存在与目标中的源名称同名的文件/文件夹。所以我们可能需要用源文件覆盖现有的目标文件。

关闭。移动() 方法用于将文件或目录从一个地方移动到另一个地方。如果目标中存在将使用os.path.isfile()os.path.isdir()方法检查的现有目录或文件,则将使用 os.remove()方法,如果它是一个目录,那么它将使用shutil.rmtree()方法删除,然后文件将被移动。

句法:

通过用字符串格式的整个路径替换源和目标来调用 shutil.move(source, destination) 方法。使用上述方法,同名文件将被源文件的文件内容覆盖

示例 1:使用Python移动包含文件的文件夹的程序。

文件夹层次结构:

Desktop
     |_folder_
              |_Geeks folder
              |_test folder_gfg.txt
Python3
# importing os module
import os
 
# importing shutil module
import shutil
 
# path
path = 'C:/Users/KIIT/Desktop/folder'
 
# List files and directories
print("Before moving file:")
print(os.listdir(path))
 
 
# Assign source and destination
source = 'test folder'
destination = 'Geeks folder'
 
sourcePath = path+'/'+source
destinationPath = path+'/'+destination
 
# Check if file already exists
if os.path.isdir(destinationPath+'/'+source):
    print(source, 'exists in the destination path!')
    shutil.rmtree(destinationPath+'/'+source)
     
elif os.path.isfile(destinationPath+'/'+source):
    os.remove(destinationPath+'/'+source)
    print(source, 'deleted in', destination)
 
# Move the content
# source to destination
dest = shutil.move(sourcePath, destinationPath)
 
# List files and directories
print("After moving file:")
print(os.listdir(path))
 
print(source, 'has been moved!')
 
# Print new path of file
print("Destination path:", dest)


Python3
# importing os module
import os
 
# importing shutil module
import shutil
 
# path
path = 'C:/Users/KIIT/Desktop/folder'
 
# List files and directories
print("Before moving file:")
print(os.listdir(path))
 
 
# Assign source and destination
source = 'test folder'
destination = 'Geeks folder'
 
sourcePath=path+'/'+source
destinationPath=path+'/'+destination
 
# Check if file already exists
if os.path.isdir(destinationPath+'/'+source):
        print(source,'exists in the destination path!')
        shutil.rmtree(destinationPath+'/'+source)
       
elif os.path.isfile(destinationPath+'/'+source):  
        os.remove(destinationPath+'/'+source)
        print(source,'deleted in',destination)
 
# Move the content
# source to destination
dest = shutil.move(sourcePath, destinationPath)
 
# List files and directories
print("After moving file:")
print(os.listdir(path))
 
print(source,'has been moved!')
 
# Print new path of file
print("Destination path:", dest)


输出:

示例 2:使用Python覆盖包含文件的文件夹的程序。

文件夹层次结构:

Desktop
     |_folder_
              |_Geeks folder_test folder_gfg.txt
              |_test folder_gfg.txt

蟒蛇3

# importing os module
import os
 
# importing shutil module
import shutil
 
# path
path = 'C:/Users/KIIT/Desktop/folder'
 
# List files and directories
print("Before moving file:")
print(os.listdir(path))
 
 
# Assign source and destination
source = 'test folder'
destination = 'Geeks folder'
 
sourcePath=path+'/'+source
destinationPath=path+'/'+destination
 
# Check if file already exists
if os.path.isdir(destinationPath+'/'+source):
        print(source,'exists in the destination path!')
        shutil.rmtree(destinationPath+'/'+source)
       
elif os.path.isfile(destinationPath+'/'+source):  
        os.remove(destinationPath+'/'+source)
        print(source,'deleted in',destination)
 
# Move the content
# source to destination
dest = shutil.move(sourcePath, destinationPath)
 
# List files and directories
print("After moving file:")
print(os.listdir(path))
 
print(source,'has been moved!')
 
# Print new path of file
print("Destination path:", dest)

输出: