python multiple inputs and multiple outputs -


i have written script in python, works on single file. couldn't find answer make run on multiple files , give output each file separately.

out = open('/home/directory/a.out','w') infile = open('/home/directory/a.sam','r')  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 chang , d here aswell             intlist = [int(x) x in match] ##            if len(intlist) < 10:             indel in intlist:                 if indel >= 10: ##                    print indel             ###intlist contains lengths of insertions in each read             #print intlist                     read_aln_start = int(samlist[3])                     indel_positions = []                     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 script take multiple files names a.sam, b.sam, c.sam , each file give me output : aout.sam, bout.sam, cout.sam

can please pass me either solution or hint.

regards, irek

i'd recommend wrapping script in function, using def keyword, , passing names of input , output files parameters function.

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

now can call function combination of input , output file names.

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

if want all files in directory, use glob library. generate output filenames, replace last 3 characters ("sam") "out".

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

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -