简体   繁体   English

Python文件不存在-验证了正确的工作目录

[英]Python file doesn't exist--verified correct working directory

Reading a csv with pandas, I get the error: 'IOError: File /test.csv does not exist.' 使用熊猫读取csv时,出现错误:“ IOError:文件/test.csv不存在。” Here is how I am reading: 这是我的阅读方式:

var1 = pd.read_csv("test.csv")

I verified that I am in the correct working directory with: 我使用以下命令验证我在正确的工作目录中:

print(os.getcwd())

Which prints C:\\Users\\name\\folderName, and the test.csv file is in folderName. 它将显示C:\\ Users \\ name \\ folderName,而test.csv文件位于folderName中。 I also check if the file exists using os.path.isfile('test.csv') and that returns false. 我还使用os.path.isfile('test.csv')检查文件是否存在,并返回false。 What am I doing incorrectly? 我做错了什么? I have tried using the full path as well, it did not work. 我也尝试过使用完整路径,但是没有用。

Edit: I tried opening a .txt file with the same format and everything was fine, so the problem seems to be with the fact that I'm opening a .csv. 编辑:我尝试打开具有相同格式的.txt文件,并且一切都很好,所以问题似乎出在我打开.csv的事实上。

The correct path would be 正确的路径是

os.path.join(os.getcwd(), test.csv)

os.getcwd() gives you the path C:\\Users\\name\\folderName and then we join the filename to make the complete path. os.getcwd()为您提供路径C:\\ Users \\ name \\ folderName,然后我们将文件名连接起来以形成完整路径。

问题是文件名“ test.csv”并键入csv而不是csv类型的“ test”。

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

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