我是Python的新手,请原谅以下基本代码和问题,但我一直在试图弄清楚导致错误的原因。
以下是我要做的事情:
这是我的代码:
import os import csv def get_all_files(directory): dir_list = os.listdir(directory) csv_files = [] for e in dir_list: if e.endswith('.csv'): csv_files.append(e) return csv_files def sum_from_csv(csv_file): cr = csv.reader(open(csv_file, 'r')) cr.next() file_content=cr.readlines() #initialize throughput total as zero throughput_total=0 #array to save throughput in every iteration throughput_dataset=[] for line in file_content: line=line.strip() data=line.split(",")[1] float_data=float(data) throughput_total+=float_data throughput_dataset.append(float_data) #to calculate number of dataset dataset_length=len(throughput_dataset) throughput_average=throughput_total/dataset_length throughput.append(throughput_average) print "Throughput-total is",throughput_total print "Average is",throughput_average csv_files = get_all_files('/home/gwthamy/Desktop/MyProject/siddhi-benchmarks/filter-4.0.0-M20/filtered-results-filter-4.0.0-M20') for each in csv_files: sum_from_csv(each)
以下是我遇到的错误:
IOError: [Errno 2] No such file or directory: 'output-0-1502441456439.csv'
我已确认文件夹和文件确实存在。是什么导致IOError以及如何解决它?另外,我的代码还有什么问题会阻止我执行整个任务吗?
提前致谢