在Python3中,可以使用`shutil`模块的`move`函数来移动文件。以下是一个简单的示例代码,展示了如何使用`move`函数移动文件:
import shutil
源文件路径
source_path = 'path/to/source/file.txt'
目标文件路径
destination_path = 'path/to/destination/file.txt'
使用shutil.move()函数移动文件
shutil.move(source_path, destination_path)
如果你需要移动整个文件夹及其内容,可以使用`shutil.move()`函数,并指定文件夹路径:
import shutil
源文件夹路径
source_folder = 'path/to/source/folder'
目标文件夹路径
destination_folder = 'path/to/destination/folder'
使用shutil.move()函数移动整个文件夹
shutil.move(source_folder, destination_folder)
如果你需要根据特定条件移动文件,比如移动特定类型的文件,可以使用`os.listdir()`和`shutil.move()`结合使用:
import os
import shutil
源文件夹路径
source_folder = 'path/to/source/folder'
目标文件夹路径
destination_folder = 'path/to/destination/folder'
遍历源文件夹
for filename in os.listdir(source_folder):
获取文件的完整路径
file_path = os.path.join(source_folder, filename)
如果文件是文件
if os.path.isfile(file_path):
获取文件的扩展名
file_extension = os.path.splitext(filename)
如果文件扩展名是你想移动的文件类型
if file_extension == '.txt':
移动文件到目标文件夹
shutil.move(file_path, os.path.join(destination_folder, filename))
请根据你的具体需求调整上述代码中的路径和条件。