打开文件时出错 - Python 3.3

时间:2013-01-26 19:22:44

标签: python file python-3.x

  

可能重复:
  IOError when trying to open existing files

我在使用python 3.3中的open()打开文件时遇到问题,不知道为什么? 我正在尝试

import os

filelist = [ f for f in os.listdir( os.curdir )]
singleFile = filelist[a]
hppfile = open(singleFile, 'r')

我得到了

FileNotFoundError: [Errno 2] No such file or directory: '-file that is actually inside the directory-'

想法?
在Windows上,我刚开始学习这个,写几个快速的脚本

1 个答案:

答案 0 :(得分:1)

如果您阅读documentation for listdir,您会看到它返回文件名而不是完整路径。

你需要像

这样的东西
current_dir_path = os.getcwd()
open(os.path.join(curren_dir_path, file), 'r')