GVKun编程网logo

Python使用开放(w +)FileNotFoundError(python open打开文件报错)

5

对于Python使用开放感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍w+FileNotFoundError,并为您提供关于certfile和keyfileFileNotFoundError:

对于Python使用开放感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍w +FileNotFoundError,并为您提供关于certfile和keyfile FileNotFoundError:[Errno 2]没有这样的文件或目录、cmd 执行 python 脚本出现 ModuleNotFoundError、FileNotFoundError: [Errno 2] No such file or directory:''D:\\a\\b''、FileNotFoundError: [Errno 2] No translation file found for domain: ''django''的有用信息。

本文目录一览:

Python使用开放(w +)FileNotFoundError(python open打开文件报错)

Python使用开放(w +)FileNotFoundError(python open打开文件报错)

  1. 创建函数saveTxtIndividualTracks(track,folder,i)基于python3.4.3和Windows 7:

    def saveTxtIndividualTracks(track,folder,i):f = open(folder+str(i)+''.txt'',''w+'')    for line in track:        l=str(line[0])+'',''+str(line[1])+'',''+str(line[2])+''\n''    f.write(l)f.close()
  2. 使用功能:

    saveTxtIndividualTracks(new,''E:/phoneTracks/''+track_name+''/'',i)

运行代码时,出现以下错误:

FileNotFoundError:[错误2]没有这样的文件或目录:’E:/phoneTracks/TA92903URN7ff/0.txt’

phoneTracks在E中创建了文件夹。我open()对mode的功能感到困惑''w+'',该模式用于创建新文件。为什么会出现FileNotFoundError?我该如何解决?

答案1

小编典典

由于目录-E:/phoneTracks/TA92903URN7ff/不存在,导致出现错误。

显示此错误的示例-

In [57]: open(''blah/abcd.txt'',''w+'')---------------------------------------------------------------------------FileNotFoundError                         Traceback (most recent call last)<ipython-input-57-46a07d4a5d18> in <module>()----> 1 open(''blah/abcd.txt'',''w+'')FileNotFoundError: [Errno 2] No such file or directory: ''blah/abcd.txt''

我的代码中出现错误,因为该目录blah/不存在。

如果目录-TA92903URN7ff/是恒定的,请尝试创建它,然后运行。如果它不是恒定的,则可以签出os.path.exists以检查目录是否存在,如果不存在,则使用创建目录os.mkdir

范例-

import os, os.pathdef saveTxtIndividualTracks(track,folder,i):    if not os.path.exists(folder):         os.mkdir(folder)    elif not os.path.isdir(folder):         return #you may want to throw some error or so.    f = open(os.path.join(folder, str(i)+''.txt''),''w+'')        for line in track:            l=str(line[0])+'',''+str(line[1])+'',''+str(line[2])+''\n''        f.write(l)    f.close()

另外,您应该考虑使用os.path.join联接路径,而不是使用字符串串联。并且还使用with-来打开文件的语句withopen(os.path.join(folder, str(i)+''.txt''),''w+'') as f:,这样,一旦with块结束,文件将自动关闭。

certfile和keyfile FileNotFoundError:[Errno 2]没有这样的文件或目录

certfile和keyfile FileNotFoundError:[Errno 2]没有这样的文件或目录

如何解决certfile和keyfile FileNotFoundError:[Errno 2]没有这样的文件或目录?

我创建了一个flask应用程序,该应用程序中也带有破折号,并且我正在Windows 64位上工作。 我已经安装了pyopenssl并在运行以下代码后

app.run_server(debug=False,ssl_context=(''cert.pem'',''key.pem''))

运行代码后,出现以下错误:

  File "~\Anaconda3\lib\ssl.py",line 1402,in wrap_socket
    context.load_cert_chain(certfile,keyfile)

FileNotFoundError: [Errno 2] No such file or directory

请帮助解决此问题,因为我是python的新手,请详细说明...

先感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

cmd 执行 python 脚本出现 ModuleNotFoundError

cmd 执行 python 脚本出现 ModuleNotFoundError

笔者近日在cmd命令行执行程序时总是报ModuleNotFoundError的错误。

笔者的python项目文件夹架构如下

笔者在run_classifier.py文件中导入了modeling.py、optimization.py和tokenization.py中的类,导入代码段如下:

 
from BERTlearning.BertTextClassification.modeling import BertModel, get_assignment_map_from_checkpoint, BertConfig
from BERTlearning.BertTextClassification.optimization import create_optimizer
from BERTlearning.BertTextClassification.tokenization import convert_to_unicode, printable_text, FullTokenizer
 

 在cmd命令行输入命令时报错如下:

 

import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

出现错误的原因是因为在cmd中执行程序,所在路径是python的搜索路径,如果涉及到import时就会出现此类错误。

文章转载:

python在cmd命令行出现ModuleNotFoundError解决方法

FileNotFoundError: [Errno 2] No such file or directory:''D:\\a\\b''

FileNotFoundError: [Errno 2] No such file or directory:''D:\\a\\b''

查看 D 目录下的文件:


 

 

 


使用 os 模块创建目录,代码如下:

path = r"D:\a\b"
isExists = os.path.exists(path)
print(isExists)
if not isExists:  # 路径不存在,即文件名不存在
    os.mkdir(path)
View Code 

然后...........FileNotFoundError: [WinError 3] 系统找不到指定的路径。: ''D:\\a\\b''


 

 

 经过一轮斗争,最后发现:D 盘中根本就没有 a 目录,就又在 a 目录下创建 b 目录,系统就会报错。所以在当前目录下,不能同时创建子目录和孙目录。正确写法如下;


 

path = r"D:\a"
os.mkdir(path)
path = r"D:\a\b"
isExists = os.path.exists(path)
print(isExists)
if not isExists:  # 路径不存在,即文件名不存在
    os.mkdir(path)
View Code

这存粹是学艺不精的问题,写此博文仅是为了提醒我自己学习要认真。还有遇到问题不要慌,耐心看看代码,查一查资料,总会找到解决办法的。

FileNotFoundError: [Errno 2] No translation file found for domain: ''django''

FileNotFoundError: [Errno 2] No translation file found for domain: ''django''

       如果你在Django创建项目测试运行中遇到这个报错那么告诉你,当你知道原因并解决了的时候回感觉多么的扯淡...

 

你只需要打开settings找到里面的LANGUAGE_CODE看一下自己是不是本地化了 就是设置在本地(TIME_ZONE是 亚洲/上海 )(不要问我为什么不是北京,国际友人就是这么定义的---上海市国际化大都市)

 

好了,成功解决了,是不是感觉很无语... 我当时只是因为在zh后面多加了个空格......

 

今天的关于Python使用开放w +FileNotFoundError的分享已经结束,谢谢您的关注,如果想了解更多关于certfile和keyfile FileNotFoundError:[Errno 2]没有这样的文件或目录、cmd 执行 python 脚本出现 ModuleNotFoundError、FileNotFoundError: [Errno 2] No such file or directory:''D:\\a\\b''、FileNotFoundError: [Errno 2] No translation file found for domain: ''django''的相关知识,请在本站进行查询。

本文标签: