主要要求:
用python语言编写代码实现以下功能:
(1)自动读取某路径下的文件,并将文件复制到另一路径下;
(2)对复制后的文件按顺序重命名,命名格式为:nw_insulator_xxx 如:nw_insulator_000,nw_insulator_001
(3)将复制后的所有文件的文件名写入TXT文件
(4)手动在TXT文件添加一文件名,通过程序将图像文件与文件名比对,删除添加的文件名
(5)随便复制一图像到图像文件路径下,通过TXT文件名将复制的图像进行删除;
import shutil, os
def get_dir(path, fileType):
'''
:param path: 路径
:param fileType: 需要复制的文件类型(.mkv或.jpg等,前面需要加.)
:return:null
'''
# 查看当前目录文件列表(包含文件夹)
allfilelist = os.listdir(path)
for file in allfilelist:
print(file, '\n')
filepath = os.path.join(path, file)
# 判断是否是文件夹,如果是则继续遍历,否则打印信息
if os.path.isdir(filepath):
allfilelist2 = os.listdir(filepath)
for file2 in allfilelist2:
filepath3 = os.path.join(filepath, file2)
# 判断文件是否以.JPG结尾
if filepath3.endswith(fileType):
print('found files:' + filepath3)
# 复制filepath3找到的文件到distPath目录
shutil.copy(filepath3, distPath)
else:
print('not a folder, continue searching...')
if __name__ == '__main__':
path = 'E:\\task1'
# 复制到distPath目录,目录需先创建
distPath = 'C:\\Users\\Tony.Hsu\\Desktop\\123'
get_dir(path, '.JPG')
import os
class ImageRename():
def __init__(self):
self.path = 'C:\\Users\\Tony.Hsu\\Desktop\\123'
def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 0
for item in filelist:
if item.endswith('.JPG'):
src = os.path.join(os.path.abspath(self.path), item
dst = os.path.join(os.path.abspath(self.path), 'nw_insulator_' + format(str(i), '0>3s') + '.JPG')
os.rename(src, dst)
print('converting %s to %s ...' % (src, dst))
i = i + 1
print('total %d to rename & converted %d jpgs' % (total_num, i))
if __name__ == '__main__':
newname = ImageRename()
newname.rename()
import os
dir1='C:\\Users\\Tony.Hsu\\Desktop\\123'#图片文件存放地址
txt1 = 'C:\\Users\\Tony.Hsu\\Desktop\\picture.txt'#图片文件名存放txt文件地址
f1 = open(txt1,'a')#打开文件流
for filename in os.listdir(dir1):
f1.write(filename.rstrip('.JPG'))#只保存名字,去除后缀.jpg
# f1.write(filename)
f1.write("\n")#换行
f1.close()#关闭文件流
import os
root_path = "C:\\Users\\Tony.Hsu\\Desktop\\123"
def contrastDir(file_dir):
jpg_list = []
lines = []
with open('C:\\Users\\Tony.Hsu\\Desktop\\picture.txt', 'r') as file_to_read:
while True:
line = file_to_read.readline()
if not line:
break
line = line.strip('\n')
lines.append(line)
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.JPG':
jpg_list.append(os.path.splitext(file)[0])
print(lines)
print(jpg_list)
#对比txt与jpg
diff = set(lines).difference(set(jpg_list))
print(len(diff))
for name in diff: #此处name相当于一个指针,不能直接对其进行操作
print("No corresponding image file", name)
print(name)
name1 = name #将name赋给一个变量后,可以对其进行操作
with open('C:\\Users\\Tony.Hsu\\Desktop\\picture.txt', 'r') as r:
lines = r.readlines()
with open('C:\\Users\\Tony.Hsu\\Desktop\\picture.txt', 'w') as w:
for l in lines:
if name1 not in l:
w.write(l)
# 对比jpg与txt
diff2 = set(jpg_list).difference(set(lines))
print(len(diff2))
for name in diff2:
print("No corresponding txt file", name)
#删除没有txt的对应的图像
os.remove(file_dir + '/' + name +'.JPG') #注意要加'/'才能找到指定路径,否则报错
return jpg_list, lines
if __name__ == '__main__':
contrastDir(root_path)
import os
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
print(root) #当前目录路径
print(dirs) #当前路径下所有子目录
print(files) #当前路径下所有非目录子文件
import os
def file_name(file_dir):
L=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.jpeg':
L.append(os.path.join(root, file))
return L
2.Python中的os.path.splitext
3.difference() 方法用于返回集合的差集,即返回的集合元素包含在第一个集合中,但不包含在第二个集合(方法的参数)中。用法:set.difference(set)。
4.修改txt文件中的一行:
with open('C:\\Users\\Tony.Hsu\\Desktop\\picture.txt', 'r') as r:
lines = r.readlines()
with open('C:\\Users\\Tony.Hsu\\Desktop\\picture.txt', 'w') as w:
for l in lines:
if name1 not in l:
w.write(l)
1.本文部分内容借鉴了其他博主的方法,具体内容可以点击链接查看,在此表示感谢。
2.python小白难免有不足错误之处,欢迎大家交流探讨。