英文:
pd.read_excel fails with no such file or directory
问题
以下是您的代码的中文翻译:
print ("接收到:", sfile)
engine = create_engine("mysql+pymysql://xxxx@localhost/xxx")
conn = engine.connect()
pre = os.path.dirname(os.path.realpath(sfile))
fname = sfile.strip()
path = os.path.join(pre, fname)
print (path)
df = pd.read_excel(path)
df.columns = df.columns.str.strip()
df.columns= df.columns.str.lower()
df.to_sql(file_basename, con=engine, if_exists='replace')
这段代码一直能正常运行,直到今天,出现了"没有这样的文件或目录"的错误。文件确实在目录中。我尝试了不同的Excel文件,但出现了相同的错误。我尝试了根据其他答案获取基于os.cwd()
的路径,但它指向了正确的目录。我尝试了r"path"+filename
,但出现了相同的错误。以上代码部分取自另一个答案,但仍然出错。我注意到在路径中使用了\\
,例如当我执行以下操作:
path = os.path.join(pre, fname)
print (path)
我得到:
C:\xxxx\xxx\test_execution_status_details.xslx
这是正确的,但当我看到错误信息时,我得到:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\\xxxx\\\xxxx\\\test_execution_status_details.xslx'
这是第一次发生这种情况。没有面向对象编程(OOP)。一切都在同一个文件中,包括函数和对函数的调用。我只能想到在更新pandas或类似库时出现了错误,因为这没有任何意义。请帮忙!
英文:
I have this code:
print ("received: ", sfile)
engine = create_engine("mysql+pymysql://xxxx@localhost/xxx")
conn = engine.connect()
pre = os.path.dirname(os.path.realpath(sfile))
fname = sfile.strip()
path = os.path.join(pre, fname)
print (path)
df = pd.read_excel(path)
df.columns = df.columns.str.strip()
df.columns= df.columns.str.lower()
df.to_sql(file_basename, con=engine, if_exists='replace')
It always worked until today, when it's throwing a no such file or directory error
The file is in the directory.
I tried different excel files, same error.
I tried to get a os.cwd()
based on other answers but it points to the right directory
I tried r"path"+filename
but same error
The code above is partly taken from another answer, but still error
I can see a \\
being used instead of the path, for i.e. when I do a
path = os.path.join(pre, fname)
print (path)
I receive:
C:\xxxx\xxx\test_execution_status_details.xslx
which is correct, but when I read the error, I get:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\\xxxx\\\xxxx\\\test_execution_status_details.xslx'
This is the first time it happened. No OOP. Everything is in the same file, including the function and the call to the function.
I can only think of an error while updating pandas or a similar library as it does not makes any sense
Halp!
答案1
得分: 2
看起来问题可能是由文件扩展名中的拼写错误引起的。在提供的路径中,文件扩展名是.xslx
,而Excel文件应为.xlsx
。请仔细检查文件扩展名。
如果代码中文件扩展名正确而问题仍然存在,请尝试使用绝对路径以确保访问正确的目录。尝试类似以下的方法:
path = r'C:\path\to\your\file\test_execution_status_details.xlsx'
df = pd.read_excel(path)
希望对您有所帮助!
英文:
It looks like the issue might be caused by a typo in the file extension. In the provided path, the file extension is .xslx
, while it should be .xlsx
for Excel files. Please double-check the file extension.
In case the file extension is correct in your code and the issue still persists, try using an absolute path to the file to ensure you are accessing the correct directory. Try something like this:
path = r'C:\path\to\your\file\test_execution_status_details.xlsx'
df = pd.read_excel(path)
I hope it helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论