Python循环行读取txt生成列表,复制图片到指定文件夹并利用列表命名
程序员文章站
2022-04-09 13:09:10
...
设计思路
- 首先读取txt文件,循环行读取保存为一个列表
- 拷贝图片并循环这个列表命名
代码实现
# imagePath = "...\\xxx.png"
# txtPath = "...\\classMember.txt"
# pastePath = ""
print("请输入txt文件路径")
txtPath=input()
print("请输入原始图片路径")
imagePath=input()
print("请输入保存图片的文件夹")
pastePath=input()
pastePath+="\\"
f_src = open(imagePath, 'rb')
members = []
with open(txtPath, 'r', encoding='utf-8') as f:
while True:
line = f.readline()
if not line:
break
line = line.strip('\n')
members.append(line)
print(members)
content = f_src.read()
for i in members:
f_copy = open(pastePath + str(i) + '.jpg', 'wb')
f_copy.write(content)
f_src.close()
f_copy.close()