2016-03-17 169 views
0

我一直在寻找一段时间,我没有任何运气。这是我的问题:我有一个网络驱动器充满了文件夹的CSV子文件夹。最终,这些csvs需要导入到数据库中。基于结构,我想从每一行中删除一行(每张表的第二行),并将其附加到另一张新表上,以创建其自己的表和表格。后来我发现Python可以实现这一点。但是,我遇到了一些问题。我一次只做这一步,所以我不会因不知道从哪里开始而感到不知所措。问题是,我找到所有的CSV,但我无法打开每一个读取任何行写入文件..我一直在使用一些其他线程作为资源,但遇到IOError:[Errno 13]没有权限: '。'在我来这里之前,我试图用尽所有的选择,但现在我已经没有时间了。我会更感激的帮助。循环浏览csvs文件夹python

下面是代码,正如你可以从我已经玩了一会儿注释中看到:

#!/usr/bin/python 
import os 
import csv 
import sys 

#output_file = sys.argv[1] 
input_path = sys.argv[1] #I would pass a '.' here for current directory on the drive 
#output_file = sys.argv[2] 


def doWhatYouWant(line): 
    print line 
    return line 
    #let the function return, not only print, to get the value for use as below 

#filewriter = csv.writer(open(output_file,'wb')) 
#This recursively opens opens .csv files and opens them 

directory = os.path.join(input_path) 
for root,dirs,files in os.walk(directory): 
    for file in files: 
     if file.endswith(".csv"): 
      f=open(input_path, 'r') 
      lines= f.readlines() 
      f.close() 

      #reader =csv.DictReader(f,delimiter=',') 
      # writer = open("testsummary.txt",'wb') 
      # writer = csv.writer(writer, delimiter=',') 

      f=open(file.txt,'w') 

      #for row in reader: 
      #  writer.writerow(row[2]) 
      # print(row[1]) 

      newline=doWhatYouWant(line) 
      f.write(newline) 
      f.close() 


      #f.close() 
      #print file 

谢谢大家对您的帮助提前。

+1

为什么你有'file.txt'?你的意思是''file.txt'吗? –

回答

0

我收到了一些帮助,并做了一些额外的研究并获得了代码工作。 感谢您的全力帮助。我相信我已经在努力为我管理。它会遍历目录中的所有文件夹,读取我想要的第二行的csv,并将所有结果写入一张表。

#!/usr/bin/python 

#imported modules that are used 

import csv 
import glob 
import os 
import sys 
import linecache 


# Where the two arguments that are passed when running the program are stored. 
input_path = sys.argv[1] #input parameter accepted when running the program 
output_file = sys.argv[2] #output parameter is the name of the our put file 

#The stored header for the .csv 
header = [] #Where I store the header to be printed later 

#Opens the output folder that will be written to with the .csv extension 
with open(output_file+'.csv','wb') as outfile: 
    filewriter = csv.writer(outfile) 
#The header written here from the list created above^
    filewriter.writerow(header) 

#The loop for the files found from the specified input that are named with a date and eyepiece information for a name 
    for input_file in glob.glob(os.path.join(input_path,'*/[0-2]*.csv')): 
     with open(input_file,'rU') as csv_file: 
      print "reading {:s}".format(input_file) #prints the names of the files processed to the console for a quick verification 
      reader_hopefully = csv.reader(csv_file, delimiter=',', quoting=csv.QUOTE_NONE) 
      next(reader_hopefully,None) #skips per line in the csv being read 
      row=next(reader_hopefully,None) #the row that is actually necessary stored in a variable 
      filewriter.writerow(row) #Writes necessary row to csv 
      csv_file.close() #Closes open csv when finished reading it 
    outfile.close() 
0

你必须计算到您的文件,如路径:

for root, dirs, files in os.walk(directory): 
    for file in files: 
     if file.endswith(".csv"): 
      f = open(os.path.join(root, file), 'r') 
1

你所得到的IOError: [Errno 13] Permission denied: '.'异常,因为你正试图打开当前目录本身,就好像它是一个可读的文本文件:

open(input_path, 'r') 

相反,你需要做这样的事情:

open(os.path.join(root, file), 'r') 

打开文件时还要考虑使用with。例如

with open(filename, 'r') as f: