簡體   English   中英

將bash腳本轉換為python

[英]Convert bash script to 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]

沒有測試,這樣的東西應該有效。

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

此代碼定義了我們將使用的兩個文件並打開它們。 第一個在讀模式下打開,第二個在寫模式下打開。 對於輸入文件中的每一行,我們將由空格分隔的第一個查找數據寫入輸出文件。

檢查Python中的讀取和寫入文件,以獲取有關Python文件的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM