Python脚本截断目录名

2024-04-25 00:22:03 发布

您现在位置:Python中文网/ 问答频道 /正文

假设有以下脚本:

import csv
import re

#this script is designed to take a list of probe files and
#generate a corresponding swarm file for blasting them against
#some user defined database.

def __main__():
    #args
    infile  = sys.argv[1] #list of sequences to run
    outfile = sys.argv[2] #location of resulting swarm file
    outdir  = sys.argv[3] #location to store results from blast run
    db      = sys.argv[4] #database to query against

    with open(infile) as fi:
        data = [x[0].strip('\n') for x in list(csv.reader(fi))]

    cmd = open(outfile, 'w')
    blast = 'module load blast/2.2.26; blastall -v 5 -b 5 -a 4 -p blastn '
    db = ' -d ' + db


    def f(x):
         input = ' -i ' + str(x)
         out = re.search('(?<=./)([^\/]*)(?=\.)', x).group(0)
         out = ' -o ' + outdir + out + '.out'
         cmd.write(blast + db + input + out + '\n')

    map(f, data)

__main__()

如果我使用:

^{pr2}$

一个来自人.cmd将是:

module load blast/2.2.26; blastall -v 5 -b 5 -a 4 -p blastn  -d x -i /data/corni
shjp/array-annotations/agilent_4x44k_human/probe-seqs/A_33_P3344603.fas -o ./A_3
3_P3344603.out

如果我使用:

python blast-probes.py /data/cornishjp/array-annotations/agilent_4x44k_mouse/probe-seq-fasta-list.csv ./mouse.cmd ./ x

一个来自鼠标.cmd将是:

module load blast/2.2.26; blastall -v 5 -b 5 -a 4 -p blastn  -d x -i /data/cornishp/array-annotations/agilent_4x44k_mouse/probe-seqs/A_51_P100327.fas -o ./A_51_P100327.out

区别在于当agilent_4x44k_的结尾是human,目录用cornishjp正确写入文件。当结尾是mouse时,目录被错误地写成cornishp,而{}则被省略。我交换了周围的一切(把人类鼠标.cmd等等)我一辈子都搞不懂。在

唯一想到的是,当我为python脚本生成参数时,我使用tab来自动完成(linux)。这会是问题吗?它正在正确读取输入文件,因为脚本将失败。在


Tags: ofcsvto脚本cmddbdatasys