FileNotFoundError:文件b'.txt'不存在

时间:2019-05-23 20:40:21

标签: python pandas file getopenfilename

我正在尝试从多个.txt文件制作一个数据框。可能很重要的一点是,那些.txt文件曾经是.dat文件,我刚刚通过将.dat更改为.txt进行了转换。无论如何,它通常会双击打开。

我的代码是:

path = "/home/marlon/Shift One/Projeto Philips/Consolidação de Arquivos/dados/17448"

files = []
for i in os.listdir(path):
    if os.path.isfile(os.path.join(path,i)):
        files.append(i)

df = pd.DataFrame()
for i in files:
    frame = pd.read_csv(i)
    df = df.append(frame)
print(df)

输出为:

...

  File "pandas/_libs/parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__

  File "pandas/_libs/parsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source

FileNotFoundError: File b'monitor_System_HumTechRoom.txt' does not exist

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您的列表“文件”仅包含目录“路径”的内容,而不包含其中文件的绝对路径。熊猫正在寻找“ monitor_System_HumTechRoom.txt”而不是“ / home / marlon / Shift One / Projeto Philips /Consolidaçãode Arquivos / dados / 17448 / monitor_System_HumTechRoom.txt”,因此是FileNotFoundError。 我是否正确理解您想将目录中的所有文件作为csvs读取到单个数据框中,然后打印该数据框?

无需循环两次。尝试这样的事情:

csvdir = "/home/marlon/Shift One/Projeto Philips/Consolidação de Arquivos/dados/17448"

dataframes = []
for csv in os.listdir(csvdir):
    fullpath = os.path.join(csvdir, csv)
    if os.path.isfile(fullpath):
        # Read a csv into a dataframe and append to a list of dataframes
        dataframe = pd.read_csv(fullpath)
        dataframes.append(dataframe)

# Concatenate all created dataframes into one
df = pd.concat(dataframes)
print(df)