以下是一个示例代码,演示了如何在两个位置之间移动文件。
import shutil
def move_file(source, destination):
try:
shutil.move(source, destination)
print("文件移动成功!")
except FileNotFoundError:
print("源文件不存在!")
except PermissionError:
print("没有权限访问文件!")
except:
print("发生未知错误!")
source_path = "/path/to/source/file.txt"
destination_path = "/path/to/destination/file.txt"
move_file(source_path, destination_path)
请确保将/path/to/source/file.txt
和/path/to/destination/file.txt
替换为实际的源文件路径和目标文件路径。
该代码使用shutil.move()
函数来移动文件。如果源文件不存在或者没有权限访问文件,会捕获相应的异常并打印错误信息。如果移动文件成功,会打印"文件移动成功!"。
上一篇:不断运行任务的用例
下一篇:不断增加的1-5数字组合