简体   繁体   English

python多个输入和多个输出

[英]python multiple inputs and multiple outputs

I have written a script in python, which works on a single file. 我已经用python编写了一个脚本,该脚本可以在单个文件上工作。 I couldn't find an answer to make it run on multiple files and to give output for each file separately. 我找不到答案来使其在多个文件上运行并分别提供每个文件的输出。

out = open('/home/directory/a.out','w')
infile = open('/home/directory/a.sam','r')

for line in infile:
    if not line.startswith('@'):
        samlist = line.strip().split()
        if 'I' or 'D' in samlist[5]:
            match = re.findall(r'(\d+)I', samlist[5]) # remember to chang I and D here aswell
            intlist = [int(x) for x in match]
##            if len(intlist) < 10:
            for indel in intlist:
                if indel >= 10:
##                    print indel
            ###intlist contains lengths of insertions in for each read
            #print intlist
                    read_aln_start = int(samlist[3])
                    indel_positions = []
                    for num1, i_or_d, num2, m in re.findall('(\d+)([ID])(\d+)?([A-Za-z])?', samlist[5]):
                        if num1:
                            read_aln_start += int(num1)
                        if num2:
                            read_aln_start += int(num2)
                        indel_positions.append(read_aln_start)
                #print indel_positions
                    out.write(str(read_aln_start)+'\t'+str(i_or_d) + '\t'+str(samlist[2])+ '\t' + str(indel) +'\n')
out.close()

I would like my script to take multiple files with names like a.sam, b.sam, c.sam and for each file give me the output : aout.sam, bout.sam, cout.sam 我希望我的脚本采用多个名称为a.sam,b.sam,c.sam的文件,并为每个文件提供输出:aout.sam,bout.sam,cout.sam

Can you please pass me either a solution or a hint. 您能给我一个解决方案还是一个提示?

Regards, Irek 问候,Irek

Loop over filenames. 循环遍历文件名。

input_filenames = ['a.sam', 'b.sam', 'c.sam']
output_filenames = ['aout.sam', 'bout.sam', 'cout.sam']
for infn, outfn in zip(input_filenames, output_filenames):
    out = open('/home/directory/{}'.format(outfn), 'w')
    infile = open('/home/directory/{}'.format(infn), 'r')
    ...

UPDATE 更新

Following code generate output_filenames from given input_filenames. 以下代码从给定的input_filenames生成output_filenames。

import os

def get_output_filename(fn):
    filename, ext = os.path.splitext(fn)
    return filename + 'out' + ext

input_filenames = ['a.sam', 'b.sam', 'c.sam'] # or glob.glob('*.sam')
output_filenames = map(get_output_filename, input_filenames)

I'd recommend wrapping that script in a function, using the def keyword, and passing the names of the input and output files as parameters to that function. 我建议使用def关键字将该脚本包装在一个函数中,并将输入和输出文件的名称作为参数传递给该函数。

def do_stuff_with_files(infile, outfile):
    out = open(infile,'w')
    infile = open(outfile,'r')
    # the rest of your script

Now you can call this function for any combination of input and output file names. 现在,您可以为输入和输出文件名的任何组合调用此函数。

do_stuff_with_files('/home/directory/a.sam', '/home/directory/a.out')

If you want to do this for all files in a certain directory, use the glob library. 如果要对某个目录中的所有文件执行此操作,请使用glob库。 To generate the output filenames, just replace the last three characters ("sam") with "out". 要生成输出文件名,只需将后三个字符(“ sam”)替换为“ out”。

import glob
indir, outdir = '/home/directory/', '/home/directory/out/'
files = glob.glob1(indir, '*.sam')
infiles  = [indir  + f              for f in files]
outfiles = [outdir + f[:-3] + "out" for f in files]
for infile, outfile in zip(infiles, outfiles):
    do_stuff_with_files(infile, outfile)

The following script allows working with an input and output file. 以下脚本允许使用输入和输出文件。 It will loop over all files in the given directory with the ".sam" extension, perform the specified operation on them, and output the results to a separate file. 它将循环扩展名为“ .sam”的给定目录中的所有文件,对其执行指定的操作,然后将结果输出到单独的文件中。

Import os
# Define the directory containing the files you are working with
path = '/home/directory'
# Get all the files in that directory with the desired
# extension (in this case ".sam")
files = [f for f in os.listdir(path) if f.endswith('.sam')]
# Loop over the files with that extension
for file in files:
    # Open the input file
    with open(path + '/' + file, 'r') as infile:
        # Open the output file
        with open(path + '/' + file.split('.')[0] + 'out.' +
                               file.split('.')[1], 'a') as outfile:
            # Loop over the lines in the input file
            for line in infile:
                # If a line in the input file can be characterized in a
                # certain way, write a different line to the output file.
                # Otherwise write the original line (from the input file)
                # to the output file
                if line.startswith('Something'):
                    outfile.write('A different kind of something')
                else:
                    outfile.write(line)
    # Note the absence of either a infile.close() or an outfile.close()
    # statement. The with-statement handles that for you

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM