2017-08-06 18 views
1

私はディレクトリツリーを歩き回り、散歩中に出会った各CSVに対してファイルを開き、カラム0と15をデータに読み込みたいと思います-frame(私が処理し、次のファイルに移動しますそのあと私は次を使用してディレクトリツリーを歩くことができます。ディレクトリツリーをスキャンし、データフレームに.csvファイルを読み込む(Python)

rootdir = r'C:/Users/stacey/Documents/Alco/auditopt/' 
for dirName,sundirList, fileList in os.walk(rootdir): 
     print('Found directory: %s' % dirName) 
     for fname in fileList: 
      print('\t%s' % fname) 
      df = pd.read_csv(fname, header=1, usecols=[0,15],parse_dates=[0], dayfirst=True,index_col=[0], names=['date', 'total_pnl_per_pos']) 
      print(df) 

が、私は、エラーメッセージになっています:

FileNotFoundError: File b'auditopt.os-pnl.BBG_XASX_ARB_S-BBG_XTKS_7240_S.csv' does not exist. 

を私は存在するファイルを読み込もうとしています。それらはMS Excelの.csv形式になっていますので、それが問題かどうかわかりません。if誰かが私にMS Excelの.csvをどのようにデータフレームpleaaeを読むのか教えてもらえますか?そうでなければ、どんな助けも大歓迎です。次のように

フルスタックトレースは次のとおりです。

Found directory: C:/Users/stacey/Documents/Alco/auditopt/ 
Found directory: C:/Users/stacey/Documents/Alco/auditopt/roll_597_oe_2017-03-10 
     tradeopt.os-pnl.BBG_XASX_ARB_S-BBG_XTKS_7240_S.csv 
Traceback (most recent call last): 

    File "<ipython-input-24-3753e367432d>", line 1, in <module> 
    runfile('C:/Users/stacey/Documents/scripts/Pair_Results_Code_1.0.py', wdir='C:/Users/stacey/Documents/scripts') 

    File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 

    File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 

    File "C:/Users/stacey/Documents/scripts/Pair_Results_Code_1.0.py", line 49, in <module> 
    main() 

    File "C:/Users/stacey/Documents/scripts/Pair_Results_Code_1.0.py", line 36, in main 
    df = pd.read_csv(fname, header=1, usecols=[0,15],parse_dates=[0], dayfirst=True,index_col=[0], names=['date', 'total_pnl_per_pos']) 

    File "C:\Anaconda\lib\site-packages\pandas\io\parsers.py", line 646, in parser_f 
    return _read(filepath_or_buffer, kwds) 

    File "C:\Anaconda\lib\site-packages\pandas\io\parsers.py", line 389, in _read 
    parser = TextFileReader(filepath_or_buffer, **kwds) 

    File "C:\Anaconda\lib\site-packages\pandas\io\parsers.py", line 730, in __init__ 
    self._make_engine(self.engine) 

    File "C:\Anaconda\lib\site-packages\pandas\io\parsers.py", line 923, in _make_engine 
    self._engine = CParserWrapper(self.f, **self.options) 

    File "C:\Anaconda\lib\site-packages\pandas\io\parsers.py", line 1390, in __init__ 
    self._reader = _parser.TextReader(src, **kwds) 

    File "pandas\parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:4184) 

    File "pandas\parser.pyx", line 667, in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:8449) 

FileNotFoundError: File b'tradeopt.os-pnl.BBG_XASX_ARB_S-BBG_XTKS_7240_S.csv' does not exist 

感謝

答えて

1

ファイルを読み込むときに、あなたは完全なパスを提供する必要があります。デフォルトではos.walkは完全なパスを提供しません。あなたはそれを自分で供給する必要があります。

os.path.joinを使用すると、これを簡単に行うことができます。

import os 
full_path = os.path.join(dirName, file) 
df = pd.read_csv(full_path, ...) 
関連する問題