0
我是Python的新手,所以請原諒以下基本代碼和問題,但我一直在試圖找出是什麼導致錯誤I我正在得到。IOError:[Errno 2]沒有這樣的文件或目錄試圖在Python中打開文件
這裏是我想要做的事:
- 遍歷一個特定的文件夾
- 迭代中特定文件夾中的每個CSV文件
- 進行計算
這裏是我的代碼:
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以及如何解決它?另外,我的代碼還有什麼問題會阻止我執行整個任務嗎?
在此先感謝
在Python中使用'raw'字符串表示路徑是一種很好的做法。 – godaygo