Python Beguiner:关于raise FileCreateError(e)xlsxwriter.exceptions.FileCreateError的错误:[Errno 2]没有这样的文件或控制器

2024-10-01 11:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将预测结果保存在特定目录中,但我遇到了此错误。我正在服务器上工作:


Traceback (most recent call last):
  File "/home/ge/kelo/anaconda3/lib/python3.7/site-packages/xlsxwriter/workbook.py", line 311, in close
    self._store_workbook()
  File "/home/ge/kelo/anaconda3/lib/python3.7/site-packages/xlsxwriter/workbook.py", line 627, in _store_workbook
    raise e
  File "/home/ge/kelo/anaconda3/lib/python3.7/site-packages/xlsxwriter/workbook.py", line 625, in _store_workbook
    allowZip64=self.allow_zip64)
  File "/home/ge/kelo/anaconda3/lib/python3.7/zipfile.py", line 1207, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: '/home/ge/kelo/eXP/Corpus/lexique_3e_res_LT/test_modelTraditionnel_lexicon_tfidf_ngramBigModel_SVC.xlsx'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "features.py", line 228, in <module>
    classify_essai(data_id,data,ylabels)
  File "features.py", line 216, in classify_essai
    output.to_excel("/home/ge/kelo/eXP/Corpus/lexique_3e_res_LT/test_modelTraditionnel_lexicon_tfidf_ngramBig{}.xlsx".format(model_name))
  File "/home/ge/kelo/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py", line 2256, in to_excel
    engine=engine,
  File "/home/ge/kelo/anaconda3/lib/python3.7/site-packages/pandas/io/formats/excel.py", line 742, in write
    writer.save()
  File "/home/ge/kelo/anaconda3/lib/python3.7/site-packages/pandas/io/excel/_xlsxwriter.py", line 195, in save
    return self.book.close()
  File "/home/ge/kelo/anaconda3/lib/python3.7/site-packages/xlsxwriter/workbook.py", line 313, in close
    raise FileCreateError(e)
xlsxwriter.exceptions.FileCreateError: [Errno 2] No such file or directory: '/home/ge/kelo/eXP/Corpus/lexique_3e_res_LT/test_modelTraditionnel_lexicon_tfidf_ngramBigModel_SVC.xlsx'

下面是我的脚本:


for model_name, model in models.items():
        y_pred = cross_val_predict(model, X_data, ylabels, cv=cv_splitter)
        all_accuracies = cross_val_score(model, X_data, ylabels, cv=cv_splitter)

        print("Model: {}".format(model_name))

        print(all_accuracies)
        print(all_accuracies.mean()) #moyenne  print("Accuracy: {}".format(accuracy_score(ylabels, y_pred)))   #
        print(all_accuracies.std())  #déterminer la variance

        print(classification_report(ylabels, y_pred, labels=labels))
        print("Accuracy: {}".format(accuracy_score(ylabels, y_pred)))
        cm = confusion_matrix(ylabels, y_pred)
        print("matrice confusion: {}".format(cm))

        # export des données
        output = pd.DataFrame()
        output['id'] = output_id
        output['Verbatim'] = Verbatim
        output['Expected Output'] = ylabels
        output['Predicted Output'] = y_pred
        # print(output.head())
        #output.to_excel("test_modelTraditionnel_lexicon_tfidf_ngram{}.xlsx".format(model_name))
        output.to_excel("/home/ge/kelo/eXP/Corpus/lexique_3e_res_LT/test_modelTraditionnel_lexicon_tfidf_ngramBig{}.xlsx".format(model_name))

如何克服这个问题,我应该在导出到特定目录之前使用os.chdir修改目录,还是有更好的解决方案


Tags: inpyhomeoutputmodellibpackagesline