用Python实现筛选文件脚本的方法

yipeiwu_com6年前Python基础

在做项目时遇到需要标记数据集里面的若干图片数据,作为程序员,为避免手动一张一张的筛选,所以写了这个Python脚本实现。

Python脚本如下:

# from PIL import Image
import csv
import os
import shutil
 
filename = 'img.txt'
 
def readImageName():
 with open(filename) as f:
  lines = f.readlines()
  imgnames = []
  for line in lines:
   imgnames.append(line.strip().strip(".jpg")[-4:])
  print(imgnames)
  return imgnames
 
def pickImg():
 pickImageNames = readImageName()
 # 遍历所有图片集的文件名
 for image in os.listdir(r"C:\Users\Administrator.PC-201708272051\Desktop\项目组\text_detect_label_data\China_SameBrowser"):
  # print(image[:-4])
  if image[:-4] in pickImageNames:
   # pickImage = Image.open((r"C:\Users\Administrator.PC-201708272051\Desktop\项目组\text_detect_label_data\China_SameBrowser/%s") % image)
   # pickImage.save((r"C:/Users/Administrator.PC-201708272051/Desktop/labeldata/%s") % image)
 
   oldname= r"C:\Users\Administrator.PC-201708272051\Desktop\项目组\text_detect_label_data\China_SameBrowser/" + image
   newname= r"C:/Users/Administrator.PC-201708272051/Desktop/labeldata/" + image
   shutil.copyfile(oldname,newname)
 
# readImageName()
pickImg()
 

以上这篇用Python实现筛选文件脚本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Python 4.0 预计推出的新功能

Python 3.8 发布在即,核心开发者团队让我总结一下最近讨论的 Python 4.0 预计推出的新功能,代码名为“ Ouroboros:自噬蛇”。Python 4.0 是大家翘首以...

用Eclipse写python程序

用Eclipse写python程序

在上一篇文章里已经写过如何安装python和在eclipse中配置python插件,这篇就不多说了,开始入门。 1.先新建一个python工程,File-->New-->Ot...

python 接口实现 供第三方调用的例子

python 接口实现 供第三方调用的例子

实验环境 1.环境问题 python 2.7 以上自带的pyunit bottle 作为一个python的简易服务器 在python安装目录 打开命令窗口(具体 shift+鼠标右键)...

在Python中操作字符串之startswith()方法的使用

 startswith()方法检查字符串是否以str开始,任选限制匹配与给定索引的开始和结束。 语法 以下是startswith()方法的语法: str.startswit...

python读文件的步骤

python读文件的步骤

python怎么读文件? 首先,在桌面上建立一个txt文档,在上面输入以下内容: 你好。Hello.abcdefg啊不错的风格 查看文件的属性,获取文件的绝对路径: D:\Hi...