2016-09-02 2 views
1

나는 bash 스크립트를 가지고 있으며이를 파이썬으로 변환하려고합니다. bash 스크립트를 python으로 변환하십시오.

는 스크립트입니다

mv $1/positive/*.$3 $2/JPEGImages 
mv $1/negative/*.$3 $2/JPEGImages 
mv $1/positive/annotations/*.xml $2/Annotations 
mv $1/negative/annotations/*.xml $2/Annotations 
cut -d' ' -f1 $1/positive_label.txt > $4_trainval.txt 

내 문제 : 어떻게 $의 4_trainval.txt 과거 positive_label.txt에 발견되지 않았다.

이것은 제 생각에는 파이썬으로 작업 한 첫 번째 시도입니다. 제발 나를 도와주세요. 감사합니다.

import sys # Required for reading command line arguments 
import os # Required for path manipulations 
from os.path import expanduser # Required for expanding '~', which stands for home folder. Used just in case the command line arguments contain "~". Without this, python won't parse "~" 
import glob 
import shutil 


def copy_dataset(arg1,arg2,arg3,arg4): 
    path1 = os.path.expanduser(arg1) 
    path2 = os.path.expanduser(arg2) # 
    frame_ext = arg3 # File extension of the patches 
    pos_files = glob.glob(os.path.join(path1,'positive/'+'*.'+frame_ext)) 
    neg_files = glob.glob(os.path.join(path1,'negative/'+'*.'+frame_ext)) 
    pos_annotation = glob.glob(os.path.join(path1,'positive/annotations/'+'*.'+xml)) 
    neg_annotation = glob.glob(os.path.join(path1,'negative/annotations/'+'*.'+xml)) 

    #mv $1/positive/*.$3 $2/JPEGImages 
    for x in pos_files: 
     shutil.copyfile(x, os.path.join(path2,'JPEGImages')) 

    #mv $1/negative/*.$3 $2/JPEGImages 
    for y in neg_files: 
     shutil.copyfile(y, os.path.join(path2,'JPEGImages')) 

    #mv $1/positive/annotations/*.xml $2/Annotations 
    for w in pos_annotation: 
     shutil.copyfile(w, os.path.join(path2,'Annotations')) 

    #mv $1/negative/annotations/*.xml $2/Annotations 
    for z in neg_annotation: 
     shutil.copyfile(z, os.path.join(path2,'Annotations')) 

    #cut -d' ' -f1 $1/positive_label.txt > $4_trainval.txt 
    for line in open(path1+'/positive_label.txt') 
     line.split(' ')[0] 

답변

0

테스트를 거치지 않고 작동해야합니다.

#cut -d' ' -f1 $1/positive_label.txt > $4_trainval.txt 
positive = path1+'/positive_label.txt' 
path4 = os.path.join(arg4, '_trainval.txt') 

with open(positive, 'r') as input_, open(path4, 'w') as output_: 
    for line in input_.readlines(): 
     output_.write(line.split()[0] + "\n") 

이 코드는 우리가 함께 작업 할 두 파일을 정의하고 두 파일을 모두 엽니 다. 첫 번째는 읽기 모드로, 두 번째는 쓰기 모드로 열립니다. 입력 파일의 각 행에 대해 출력 파일에 공백으로 구분 된 첫 번째 찾기 데이터를 씁니다.

파이썬에 대한 파일에 대한 자세한 내용은 Reading and Writing Files in Python을 확인하십시오.

관련 문제