2017-08-11 6 views
0

私はPythonの初心者ですので、以下の基本的なコードと問題は許してください。取得しています。ここでIOError:[Errno 2] Pythonでファイルを開こうとしているファイルやディレクトリがありません

は私がしようとしているものです:

  1. ループの特定のフォルダから、指定したフォルダ
  2. の各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の原因と解決方法また、私のコードには、タスク全体を実行することを妨げる何か他の問題がありますか?

    ありがとうございます。

  • +0

    Pythonでパスを表すために 'raw'文字列を使用することをお勧めします。 – godaygo

    答えて

    1

    これは動作するはずです!

    import os 
    
    dir = '/home/gwthamy/Desktop/MyProject/siddhi-benchmarks/filter-4.0.0-M20/filtered-results-filter-4.0.0-M20' 
    for each in get_all_files(dir): 
        sum_from_csv(os.path.join(dir, each)) 
    
    +0

    ありがとうございますそれは正常に動作します –

    関連する問題