Python – 移动和覆盖文件和文件夹
在本文中,我们将学习如何移动文件和文件夹的集合,其中可能存在与目标中的源名称同名的文件/文件夹。所以我们可能需要用源文件覆盖现有的目标文件。
这 关闭。移动() 方法用于将文件或目录从一个地方移动到另一个地方。如果目标中存在将使用os.path.isfile()和os.path.isdir()方法检查的现有目录或文件,则将使用 os.remove()方法,如果它是一个目录,那么它将使用shutil.rmtree()方法删除,然后文件将被移动。
句法:
shutil.move(source, destination, copy_function = copy2)
Parameters:
- source: A string representing the path of the source file.
- destination: A string representing the path of the destination directory.
- copy_function (optional): The default value of this parameter is copy2. We can use other copy function like copy, copytree, etc for this parameter.
Return Value: This method returns a string which represents the path of newly created file.
通过用字符串格式的整个路径替换源和目标来调用 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)
输出: