Home >Backend Development >Python Tutorial >What should I do if python open cannot find the file?

What should I do if python open cannot find the file?

coldplay.xixi
coldplay.xixiOriginal
2020-06-22 10:45:367344browse

What should I do if python open cannot find the file?

推荐教程:《python视频教程

python open找不到文件怎么办?

python open找不到文件的解决办法:

 在python和很多程序语言中"\"转义符号,要想输出\有两种方法,一是多加一个\写成\\ ,一是在字符串前加一个r,提示解释器按原始字符处理

  解决方法1代码:  

 try:
    source=open('D:\eclipse-workspace\AcrSoftware\data\\filename.txt','r',encoding='utf-8')
  except IOError:    print('Error:没有找到 文件或读取文件失败')

  解决方法2代码:   

 try:
    source=open(r'D:\eclipse-workspace\AcrSoftware\data\filename.txt','r',encoding='utf-8')
  except IOError:    print('Error:没有找到 文件或读取文件失败')

推荐相关文章:《python教程

The above is the detailed content of What should I do if python open cannot find the file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn