我一直在寻找,但我没有运气。这是我的问题:我的网络驱动器中装有包含CSV子文件夹的文件夹。最终,这些csv需要导入数据库。基于该结构,有一行(每张纸的第二行)我想从每一行中删除并附加到一张新纸张上,以创建自己的纸张和表格。前段时间我发现Python可以实现这一目标。但是,我遇到了一些问题。我一步一步这样做,所以不知道从哪里开始我不会感到不知所措。问题是我找到了所有的CSV,但我无法打开每一个读取任何行来写入文件。我一直在使用其他一些线程作为资源,但遇到了 IOError:[Errno 13]许可被拒绝:'。' 在我来到这里之前,我试图用尽所有的选择,但现在我已经没时间了。我非常感谢你的帮助。
以下是代码,您可以从我一直播放的评论中看到:
#!/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
提前感谢大家的帮助。
答案 0 :(得分:1)
您收到IOError: [Errno 13] Permission denied: '.'
异常,因为您尝试打开当前目录本身,就像它是一个可读的文本文件一样:
open(input_path, 'r')
相反,你需要做这样的事情:
open(os.path.join(root, file), 'r')
打开文件时还要考虑使用with
。例如
with open(filename, 'r') as f:
答案 1 :(得分: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')
答案 2 :(得分: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()