栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Python】图片搬家demo例子

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Python】图片搬家demo例子

代码作用

把指定名字的图片从地址A粘贴到地址B

代码
import os
import shutil

def read_txt(txt_name):
    with open(txt_name, 'r', encoding='utf-8') as f:
        txt_list = f.readlines()
    for i in range(len(txt_list)):
        txt_list[i] = txt_list[i].rstrip('n')
    return txt_list

def get_img_path(txt_list):
    src_dir = txt_list.pop(0)
    dst_dir = txt_list.pop(0)
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir, exist_ok=True)
    src_img_path = [os.path.join(src_dir, img_dir) for img_dir in txt_list if img_dir != '']
    dst_dir_path = [os.path.join(dst_dir, img_dir) for img_dir in txt_list if img_dir != '']
    return src_img_path, dst_dir_path

# 复制图片
def copy_img(src_img_path, dst_dir_path):
    for src_img, dst_dir in zip(src_img_path, dst_dir_path):
        try:
            shutil.copy(src_img, dst_dir)
        except:
            print('【失败】{}【请检查图片是否存在于源地址】'.format(src_img))
    print('【其余图片复制成功】')
    input('n【运行完成,请按回车键退出】')

if __name__ == '__main__':
    # 读取地址
    txt = 'img.txt'
    try:
        txt_list = read_txt(txt)
        # 读取图片
        src_img_path, dst_dir_path = get_img_path(txt_list)
        # 复制图片
        copy_img(src_img_path, dst_dir_path)
    except:
        input('【未在当前目录下找到img.txt,请按照样例新建img.txt】')
如果打包成exe

pyinstall安装地址地址:https://www.cnblogs.com/mufenglin/p/7479281.html

在cmd窗口运行

pyinstaller -F remove_image.py

所需要的exe就在生成的dist文件夹下。
本例子exe下载地址:图片搬家demo例子.zip

参考文件

pyinstall使用:
https://www.cnblogs.com/mufenglin/p/7479281.html
https://www.crifan.com/use_pyinstaller_to_package_python_to_single_executable_exe/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354932.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号