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') #修改當前工作目錄

文件名要帶后綴

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM