無法按順序讀取csv文件

我想建立一個文件夾中所有csv文件的列表,并將它們讀入數據幀。所以我按照這個問題,并能夠得到所有的csv文件在一個理想的文件夾。然后,當我嘗試迭代地讀取每個文件夾時,會得到一個錯誤,即特定的文件夾不存在。

import os

directory = os.path.join("/home/5G-production-dataset/5G-production-dataset/Download","Driving/")

print(directory) ## works

df_final = pd.DataFrame() ## each dataframe read will be concatenated to this

for root,dirs,files in os.walk(directory):
    print(f"files = {files}") ## gives a list with all the filenames as elements
    for file in files:
        print(f"current file is {file} and the type is {type(file)}") ## prints the current file and dtype is string
        if file.endswith(".csv"):

           df = pd.read_csv(file) ## error that the files doess't exist
           print(len(df))
           df_final = pd.concat([df], axis = 0, ignore_index=True)


print(len(df_final))

我驗證了文件名,它是從列表中包含的所有文件,我想讀。完整的錯誤是

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-17-65d96982fe32> in <module>
     13         #    f=open(file, 'r')
     14            #  perform calculation
---> 15            df = pd.read_csv(file)
     16            print(len(df))
     17            df_final = pd.concat([df], axis = 0, ignore_index=True)

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    683         )
    684 
--> 685         return _read(filepath_or_buffer, kwds)
    686 
    687     parser_f.__name__ = name

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    455 
    456     # Create the parser.
--> 457     parser = TextFileReader(fp_or_buf, **kwds)
    458 
    459     if chunksize or iterator:

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    893             self.options["has_index_names"] = kwds["has_index_names"]
    894 
--> 895         self._make_engine(self.engine)
    896 
    897     def close(self):

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
   1133     def _make_engine(self, engine="c"):
   1134         if engine == "c":
-> 1135             self._engine = CParserWrapper(self.f, **self.options)
   1136         else:
   1137             if engine == "python":

~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
   1915         kwds["usecols"] = self.usecols
   1916 
-> 1917         self._reader = parsers.TextReader(src, **kwds)
   1918         self.unnamed_cols = self._reader.unnamed_cols
   1919 

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] File b'B_2019.12.16_14.23.32.csv' does not exist: b'B_2019.12.16_14.23.32.csv'

這可能是由于文件名有多個點(.)符號造成的嗎?我有很多文件,不能手動檢查。

? 最佳回答:

看起來您需要指定相對于腳本本身的文件路徑:

df = pd.read_csv(os.path.join(root, file))
主站蜘蛛池模板: 成人精品一区二区三区校园激情 | 精品国产一区二区三区AV性色| 亚洲AV噜噜一区二区三区| 久久亚洲色一区二区三区| 一区二区亚洲精品精华液| 中文字幕永久一区二区三区在线观看 | 国产91精品一区二区麻豆网站| 精品视频在线观看你懂的一区| 国模一区二区三区| 亚洲一区AV无码少妇电影| 久久一区二区三区精品| 国产综合无码一区二区辣椒| 免费一区二区无码视频在线播放| 在线观看精品视频一区二区三区| 中文字幕日韩一区| 人妻无码一区二区不卡无码av| 国99精品无码一区二区三区| 国产日韩一区二区三免费高清 | V一区无码内射国产| 欧洲精品一区二区三区在线观看| 国精产品一区一区三区MBA下载| 91精品一区二区三区久久久久| 一区二区三区四区免费视频| 亚洲狠狠久久综合一区77777 | 一区二区三区精品高清视频免费在线播放| 亚洲AV成人一区二区三区观看 | 国产美女在线一区二区三区| 无码国产精品一区二区免费| 亚洲一区二区三区乱码在线欧洲| 无码人妻精品一区二区三区99性| 亚拍精品一区二区三区| 午夜AV内射一区二区三区红桃视| 无码人妻一区二区三区免费手机| 中文字幕精品一区二区| 在线观看日韩一区| 中文字幕一区二区三区在线播放| 国产成人无码AV一区二区在线观看 | 蜜臀AV无码一区二区三区| 午夜视频一区二区| 国产伦精品一区二区三区视频猫咪| 综合久久一区二区三区|