Skip to content

Instantly share code, notes, and snippets.

@PingjunChen
Last active June 9, 2018 04:31
Show Gist options
  • Save PingjunChen/f669c0456cb789f0e58c3c4cbeb3ef9c to your computer and use it in GitHub Desktop.
Save PingjunChen/f669c0456cb789f0e58c3c4cbeb3ef9c to your computer and use it in GitHub Desktop.
Parsing txt file
# -*- coding: utf-8 -*-
__author__ = "Pingjun Chen"
__email__ = "chenpingjun@gmx.com"
import os, sys, pdb
def organize_images(txt_path, image_path):
with open(txt_path) as f:
content = f.readlines()
for ele in content:
ele = ele.strip('\n')
desp = ele.split("\t")
category = desp[1]
filename = desp[0]
file_dir = os.path.join(image_path, category)
if not os.path.exists(file_dir):
os.mkdir(file_dir)
src_file_path = os.path.join(image_path, filename)
dst_file_path = os.path.join(file_dir, filename)
os.rename(src_file_path, dst_file_path)
if __name__ == "__main__":
txt_path = "./val_annotations.txt"
image_path = "./images"
organize_images(txt_path, image_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment