【问题标题】:Errno 2: No such file or directory: Python [duplicate]Errno 2:没有这样的文件或目录:Python [重复]
【发布时间】:2014-11-12 21:12:49
【问题描述】:

我正在编写一个循环来使用 matplotlib 绘制数据。开始,我有

path = os.listdir('/users/me/path_to_folder_with_data')
for file in path:
    code that I know works for plotting data

但是当我运行它时,我得到一个错误,上面写着[Errno 2] No such file or directory: 'data1.txt' 它是否试图从保存程序的文件夹中运行?这似乎是此处类似问题的解决方案,但如果这是真的,为什么它不从我的路径中运行?

【问题讨论】:

  • 人们在这个网站上投票的速度如此之快...... OP发布了相关代码的小sn-p以及出现的错误。不确定一个问题能得到多少更简洁的答案。
  • 我没有投反对票,但给出错误的是 code that I know works for plotting data
  • @Cyber​​ 这个问题不包括 a) 实际上 引发错误的代码 b) 完整的回溯。不难猜出问题出在哪里,但这并不是一个好问题。

标签: python matplotlib data-analysis


【解决方案1】:

你需要加入目录的路径

location = '/users/me/path_to_folder_with_data'
for file in os.listdir(location):
    fullPath = os.path.join(location, file)
    # now try to open fullPath

【讨论】:

  • 我在路径之前用 os.chdir('/users/me/path_to_folder_with_data') 尝试过它,它不再给我错误了。我认为这也有效,谢谢!
  • 如果你不需要它,为什么要chdir
猜你喜欢
  • 2014-06-18
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多