python中os.open,报错:No such file or directory:

with open('./matmul.py') as f:
  try:
    while True:
      line=next(f)
      print(line)
  except StopIteration:
       pass


报错:

IOError: [Errno 2] No such file or directory: './matmul.py'

解决:

os.open是针对当前工作目录中的文件进行打开操作的,需要修改工作目录之后再运行这个方法

 

import os

print os.getcwd()#显示当前工作目录

os.chdir('the_path_of_the_file_whitch_you_want_to_open.txt') #修改当前工作目录

文件名要带后缀

 

posted @ 2015-11-24 19:42  啤酒与炸鸡  阅读(9293)  评论(0编辑  收藏  举报