python实现拷贝指定图片到另一个路径
程序员文章站
2024-03-24 11:25:22
...
import os
import shutil
dir = os.getcwd()
print(dir)
filename = "path.txt"
path = os.path.join(dir,filename)
print(path)
f2 = open(path,'r')
for item in f2:
res = item.split("=")
if res[0] == "src":
src = res[1]
print(src)
if res[0] == "dst":
dst = res[1]
print(dst)
mydir =src[:-1]
dstdir = dst[:-1]
if not os.path.exists(dstdir):
os.makedirs(dstdir)
files = os.listdir(mydir)
for file in files:
filepath = os.path.join(mydir,file)
if os.path.isdir(filepath):
jpgs = os.listdir(filepath)
for jpg in jpgs:
if "a_left" in jpg or "a_right" in jpg:
dstpath = os.path.join(dstdir, file)
if not os.path.exists(dstpath):
os.makedirs(dstpath)
jpgpath= os.path.join(filepath,jpg)
print(jpgpath)
jpgdst = os.path.join(dstpath,jpg)
print(jpgdst)
shutil.copy(jpgpath, jpgdst)