2016-03-17 117 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: