json文件生成by python

import subprocess
import multiprocessing
import os
from os import path
import shutil
import argparse

import sys
from multiprocessing.dummy import Pool as ThreadPool

import sys
import time
f=open("LPP.txt","w+")#输出的文本,可改成json
sys.stdout=f




def getFile(Files_list):#获取文件夹

    workpath=path.dirname(__file__)  # path为文件夹路径
    file_list = os.listdir(workpath)  # file_list即为路径下文件夹名或文件名
    for files in file_list:
        fulldirct =os.path.join(workpath,files)
        if os.path.isdir(fulldirct):
            Files_list.append(files)
    #print(Files_list)


def getPicture(path, list_name): #获取文件夹下的图片
    for files in os.listdir(path): 
        file_path = os.path.join(path, files) 
        if os.path.splitext(file_path)[1]=='.jpg': 
            list_name.append(files) 
    #print(Picture_list)

def getTxt(path):#获取txt中的第一行数据
    file_path = path+"\\"+"groundtruth_rect.txt" 
    with open(file_path, "r") as f:
        line =f.readlines()
        firstline=line[0].strip('\n')

        sub_str = firstline.split('\t')
        sub_str=[x for x in sub_str if x!='']
    print("[",end="")
    for s in range(0,len(sub_str)):
        if s!=0:
            print(", ",end="")
        print(sub_str[s],end="")
    print("]",end="")
        

    
    #print(sub_str)



workpath=path.dirname(__file__)
#testpath=workpath+"\\"+"lp.txt"
#temp_file=open(testpath,'w')

Files_list=list()

getFile(Files_list)
#for File in Files_list:
print("{",end="")
for i in range(0,len(Files_list)):
	Picture_list=list()
    if i!=0:
        print(", ",end="")
    print("\"",end="")
    print(Files_list[i],end="")
    print("\": {\"video_dir\": \"",end="")
    print(Files_list[i],end="")
    print("\", \"img_names\": [",end="")
    Filepath=os.path.join(workpath,Files_list[i])
    getPicture(Filepath,Picture_list)
    for j in range(0,len(Picture_list)):
        if j!=0:
            print(", ",end="")
        print("\"",end="")
        print(Files_list[i],end="")
        print("/",end="")
        print(Picture_list[j],end="")
        print("\"",end="")
    print("], \"init_rect\": ",end="")
    #print("[",end="")
    getTxt(Filepath)
    print(", \"gt_rect\": [",end="")
    for k in range(0,len(Picture_list)):
        if k == 0:
            getTxt(Filepath)
        else:
            print(", ",end="")
            print("[0, 0, 0, 0]",end="")
    print("]}",end="")
print("}",end="")

    #for j in range(0,len(Picture_list)):
        
    

 

你可能感兴趣的:(python,python)