IOError:[Errno 2]没有这样的文件或目录:但是文件在那里... [英] IOError: [Errno 2] No such file or directory: but the files are there...

查看:251
本文介绍了IOError:[Errno 2]没有这样的文件或目录:但是文件在那里...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果确实在下面的#循环中注释了文件名,它将为您提供目录中的所有文件名.但是,当我调用pd.ExcelFile(filename)时,它返回的文件名为:[[第一个以'.xlsx'结尾的文件.我缺少什么? p.s:下面的缩进是正确的,if在我的代码中位于for之下,但此处未以这种方式显示.

If you do print filename in the for loop #commented below, it gives you all the file names in the directory. yet when I call pd.ExcelFile(filename) it returns that there is no file with the name of : [the first file that ends with '.xlsx' What am I missing? p.s: the indentation below is right, the if is under the for in my code, but it doesn't show this way here..

for filename in os.listdir('/Users/ramikhoury/PycharmProjects/R/excel_files'):
if filename.endswith(".xlsx"):
    month = pd.ExcelFile(filename)
    day_list = month.sheet_names
    i = 0
    for day in month.sheet_names:
        df = pd.read_excel(month, sheet_name=day, skiprows=21)
        df = df.iloc[:, 1:]
        df = df[[df.columns[0], df.columns[4], df.columns[8]]]
        df = df.iloc[1:16]
        df['Date'] = day
        df = df.set_index('Date')
        day_list[i] = df
        i += 1

    month_frame = day_list[0]
    x = 1
    while x < len(day_list):
        month_frame = pd.concat([month_frame, day_list[x]])
        x += 1

    print filename + ' created the following dataframe: \n'
    print month_frame  # month_frame is the combination of the all the sheets inside the file in one dataframe !

推荐答案

问题是您的工作目录与您列出的目录不同.因为您知道目录的绝对路径,所以最简单的解决方案是将os.chdir('/Users/ramikhoury/PycharmProjects/R/excel_files')添加到文件的顶部.

The problem is that your work directory is not the same as the directory you are listing. Since you know the absolute path of the directory, the easiest solution is to add os.chdir('/Users/ramikhoury/PycharmProjects/R/excel_files') to the top of your file.

这篇关于IOError:[Errno 2]没有这样的文件或目录:但是文件在那里...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆