代码作用
把指定名字的图片从地址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/
|