简体   繁体   English

尽管列出了绝对路径,但在Ubuntu中找不到文件错误

[英]File not found error in Ubuntu despite listing absolute path

I have this code snippet: 我有以下代码片段:

import xlrd
import os

path = os.getcwd()
loc = (path+"/file.xlsx")

wb = xlrd.open_workbook(loc)

While this works fine on my Mac, it does not work when I try to run it on Ubuntu, and I'm not sure why. 虽然这在我的Mac上可以正常工作,但是当我尝试在Ubuntu上运行它时却无法正常工作,我不确定为什么。 I've verified that the path is correct using pwd and ls in the terminal. 我已经在终端中使用pwdls验证了路径正确。 The python file is in the same folder as the spreadsheet I'm trying to open. python文件与我要打开的电子表格位于同一文件夹中。 I've also tried some solutions in Python, that have not worked either: 我还尝试了Python中的一些解决方案,这些解决方案也不起作用:

path = os.getcwd()
loc = os.path.join(path, "file.xlsx")

Here is the exact error I am getting: 这是我得到的确切错误:

IOError: [Errno 2] No such file or directory: '/home/rips/Desktop/script/file.xlsx'

如果python脚本和文件位于同一文件夹中,则不应使用任何路径前缀将其打开,因为python“模块”(在本例中为脚本)只是开始在同一文件夹中查找。

My code wasn't working because it turns out the excel file had been corrupted. 我的代码无法正常工作,因为事实证明excel文件已损坏。 I didn't initially suspect this as I downloaded it as an email attachment from myself on another machine. 起初我并不怀疑这是因为我以电子邮件附件的形式从另一台计算机上下载了该附件。

For some reason, using this code brought up a different error (that was ultimately more useful in diagnosing the problem): 出于某种原因,使用此代码会引发另一个错误(最终在诊断问题中更有用):

loc = os.path.join(os.path.expanduser('~'), 'Desktop', 'script', 'file.xlsx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM