2016-09-02 27 views
1

私はbashスクリプトを持っています。これをPythonに変換したいのですが。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に見つかりませんでした。

これは私の試みです。これは私がPythonで作業する初めてのことです。それを機能させるために私を助けてください。 ありがとうございます。

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") 

このコードでは、2つのファイルを使用して作業し、両方を開きます。最初のモードは読み取りモードで開き、2番目のモードは書き込みモードで開きます。 入力ファイル内の各行について、最初に見つけたデータを出力ファイルにスペースで区切って書き込みます。

Pythonへのファイルの詳細については、Reading and Writing Files in Pythonを参照してください。

関連する問題