2013-04-18 87 views
-1

我已经用Python编写了一个比较3个excel文件的脚本。我有很多我想要使用这个脚本的文件,我希望它们以三个一组的形式使用。这是它的外观至今约:Python:比较几个文件

#!/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 are the 3 files it uses at the 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 into an excel file as well 

我一般是新的节目,我希望我做的意义,当我试图解释什么,我想做的事情。欢呼任何帮助!

+0

使用'os.path.join'为concatening路径 –

+0

什么是问题的所有文件的列表? –

+0

将文件名的一部分作为参数是一个糟糕的接口,您应该让用户直接在命令行上提供工作目录的相对路径,这允许他们使用shell的完成和匹配功能而不是输入部分文件名。 – geoffspear

回答

1

我会用glob来获取所有的文件名,对它们进行排序,然后再通过他们在一个循环中(完全按照我回答here

# other imports 
from glob import glob 

# lots of functions 

resultsdir = "." 
counter = 0 
outname = sys.argv[1] 
files = sorted(glob(resultsdir+'/*.csv')) # get and sort .csv files 
while len(files) >= 3: # Are there another 3 files? 
    out = open(outname+'_'+str(counter)+'.csv',"w") # open an output file with an increasing number in name 
    counter += 1 # increase output file number 
    file1=open(files.pop(0)) # get and remove first file from the list 
    file2=open(files.pop(0)) # get the next file from the list (is now the first) 
    file3=open(files.pop(0)) 

    # do something with the files 

    # close the files 

第二个选项是使用(如在上述评论)使用

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

拿到文件,那么你必须这样调用脚本:

program.py output_name *.csv 

程序然后获取匹配通配符(*.csv)作为参数