使用Python脚本将图片文件移动到指定文件夹

在Python中,你可以使用内置的shutil模块来移动文件,包括图片。shutil.move()函数可以方便地将文件从一个位置移动到另一个位置。以下是一个简单的示例,展示如何将指定图片移动到指定目录:

图片[1]_使用Python脚本将图片文件移动到指定文件夹_知途无界
import shutil
import os
def move_image(source_path, destination_dir):
"""
将指定图片移动到指定目录。
:param source_path: 源图片文件的完整路径。
:param destination_dir: 目标目录的完整路径。
"""
try:
# 确保目标目录存在,如果不存在则创建它
os.makedirs(destination_dir, exist_ok=True)
# 获取源图片的文件名
file_name = os.path.basename(source_path)
# 构建目标文件的完整路径
destination_path = os.path.join(destination_dir, file_name)
# 移动图片文件
shutil.move(source_path, destination_path)
print(f"图片已成功移动到: {destination_path}")
except Exception as e:
print(f"移动图片时出错: {e}")
# 示例用法
source_image_path = "C:/path/to/your/image.jpg" # 替换为你的源图片路径
destination_directory = "C:/path/to/destination_folder" # 替换为你的目标目录路径
move_image(source_image_path, destination_directory)
import shutil
import os

def move_image(source_path, destination_dir):
    """
    将指定图片移动到指定目录。

    :param source_path: 源图片文件的完整路径。
    :param destination_dir: 目标目录的完整路径。
    """
    try:
        # 确保目标目录存在,如果不存在则创建它
        os.makedirs(destination_dir, exist_ok=True)
        
        # 获取源图片的文件名
        file_name = os.path.basename(source_path)
        
        # 构建目标文件的完整路径
        destination_path = os.path.join(destination_dir, file_name)
        
        # 移动图片文件
        shutil.move(source_path, destination_path)
        
        print(f"图片已成功移动到: {destination_path}")
    except Exception as e:
        print(f"移动图片时出错: {e}")

# 示例用法
source_image_path = "C:/path/to/your/image.jpg"  # 替换为你的源图片路径
destination_directory = "C:/path/to/destination_folder"  # 替换为你的目标目录路径

move_image(source_image_path, destination_directory)
import shutil import os def move_image(source_path, destination_dir): """ 将指定图片移动到指定目录。 :param source_path: 源图片文件的完整路径。 :param destination_dir: 目标目录的完整路径。 """ try: # 确保目标目录存在,如果不存在则创建它 os.makedirs(destination_dir, exist_ok=True) # 获取源图片的文件名 file_name = os.path.basename(source_path) # 构建目标文件的完整路径 destination_path = os.path.join(destination_dir, file_name) # 移动图片文件 shutil.move(source_path, destination_path) print(f"图片已成功移动到: {destination_path}") except Exception as e: print(f"移动图片时出错: {e}") # 示例用法 source_image_path = "C:/path/to/your/image.jpg" # 替换为你的源图片路径 destination_directory = "C:/path/to/destination_folder" # 替换为你的目标目录路径 move_image(source_image_path, destination_directory)

在这个示例中:

  1. source_path 是你想要移动的源图片文件的完整路径。
  2. destination_dir 是你想要将图片移动到的目标目录的完整路径。
  3. os.makedirs(destination_dir, exist_ok=True) 确保目标目录存在。如果目录不存在,它将创建该目录。如果目录已经存在,exist_ok=True 参数将防止抛出异常。
  4. os.path.basename(source_path) 获取源图片的文件名。
  5. os.path.join(destination_dir, file_name) 构建目标文件的完整路径。
  6. shutil.move(source_path, destination_path) 将图片从源路径移动到目标路径。

请确保替换示例用法中的 source_image_path 和 destination_directory 为你实际的文件路径和目录路径。如果路径中包含非ASCII字符或空格,请确保路径字符串被正确地引用或转义。

© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞14 分享
People do a lot of thinking, and sometimes, that's what kills us.
有时候是我们自己想太多才让自己如此难受
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容