繁体   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