csv - Python: comparing several files -


i have written script in python compares 3 excel files. have lots of files want use script on , want them being used in groups of three. how looks far approximately:

#!/usr/bin/env python  import sys import csv  #lots of functions  resultsdir = "." #bla  filename1=sys.argv[1] filename2=sys.argv[2] filename3=sys.argv[3] out = open(sys.argv[4],"w")  #filename1,filename2,filename3="cnvb_reads.403476","cnvb_reads.403447","cnvb_reads.403478" #these 3 files uses @ moment  file1=open(resultsdir+"/"+filename1+".csv") file2=open(resultsdir+"/"+filename2+".csv")  file3=open(resultsdir+"/"+filename3+".csv")  file1.readline()     file2.readline() file3.readline()  #lots of other irrelevant stuff #the output goes excel file 

i'm new programming in general, hope i'm making sense when try explain want do. cheers help!

i use glob filenames, sort them , go through them in loop (exactly answered here)

# other imports glob import glob  # lots of functions  resultsdir = "." counter = 0 outname = sys.argv[1] files = sorted(glob(resultsdir+'/*.csv')) # , sort .csv files while len(files) >= 3: # there 3 files?     out = open(outname+'_'+str(counter)+'.csv',"w") # open output file increasing number in name     counter += 1 # increase output file number     file1=open(files.pop(0)) # , remove first file list     file2=open(files.pop(0)) # next file list (is first)     file3=open(files.pop(0))      # files      # close files 

a second option use (as said in comments) use

files = sorted(sys.argv[2:]) 

to files, have call script this:

program.py output_name *.csv 

the program gets list of files matching wildcard (*.csv) arguments


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -