【问题标题】:Visual Studio C++ and Python, working with Input file, FileNotFoundError [Errno 2] No such file or directoryVisual Studio C++ 和 Python,使用输入文件,FileNotFoundError [Errno 2] No such file or directory
【发布时间】:2022-12-11 01:35:37
【问题描述】:

我正在使用 Visual Studio 处理 C++、Python 文件和 .txt 输入文件。我收到以下错误:

Traceback (most recent call last):
 File "C:\Users\Admin\source\repos\Project3Week7\Release\PythonCode.py", line 8, in CountAll
    text =open ("ProjectThreeInput.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'ProjectThreeInput.txt'

我的输入文件名为 ProjectThreeInput.txt。它位于我的 C++ 项目的发布文件夹中。谁能指出我为什么会收到此错误的正确方向?

这是我的 Python 代码:

import re
import string
import os.path
from os import path

#open read me file
def CountAll():
    text =open ("ProjectThreeInput.txt","r")

    # empty dictionary, remove white space, convert to lowercase, check if in dict, print functions, close file
    dictionary = dict()
    for line in text:
        line = line.strip()
        word = line.lower()
        
        if word in dictionary:
            dictionary[word] = dictionary[word] + 1
        else:
            dictionary[word] = 1

     #print dicitonary
    for key in list (dictionary.keys()):
        print(key.capitalize(), ":", dictionary[key])

     #close file
    text.close()


#word count, convert to lower case, open file, convert to lower case, check for word return count, close
def CountInstances(serchTerm):
    searchTerm = searchTerm.lower
    text = open("ProjectThreeInput.txt","r")
    wordCount = 0
    
    for line in text:
        line = line.strip()
        word = line.lower()
        if word == searchTerm:
            wordCount +=1
    return wordCount
    text.close()

# frequency.dat, open file, create empty dict, store words, remove spaces, convert to lower case, check if in dict, write key to freq.dat, close
def CollectData():
    text = open("ProjectThreeInput.txt","r")
    frequency = open("frequency.dat", "w")
    dictionary = dict()

    for line in text:
        line = line.strip()
        word = line.lower()
        if word in dictionary:
            dictionary[word] = dictionary[word] + 1
        else:
            dictionary[word] = 1

    for key in list (dictionary.keys()):
        frequency.write(str(key.capitaize()) + " " + str(dictionary[key]) + "\n")

    text.close()
    frequency.close()

【问题讨论】:

  • 您从哪个目录运行代码?该目录是否有文件ProjectThreeInput.txt
  • 是的,该输入文件与我的其他文件(包括 pythoncode 文件)位于同一目录(发布文件夹)中
  • 您是从发布文件夹中运行 python 文件吗?您也可以尝试提供ProjectThreeInput.txt 的绝对路径。
  • Visual Studio 和 Visual Studio Code 是非常不同的工具。你在这里用的是哪一个?
  • 视觉工作室。抱歉,我没意识到我标记错了。

标签: python c++ visual-studio-2019


【解决方案1】:

默认情况下,Visual Studio 在项目目录中启动 C++ 程序,其中包含 .vcxproj* 文件,而不是生成可执行文件的位置。您可以在配置属性 > 调试 > 工作目录下的项目属性中配置该目录。在您的情况下,将其设置为$(输出路径).请务必在对话框顶部选择“所有配置”(以及所有平台,如果有多个)。此设置存储在 .vcxproj.user 文件中。

或者,您可以通过更改 Configuration Properties > General > Output Directory 在保存输入文件的目录中生成可执行文件。但随后您可能会混淆来自调试和发布配置的工件。相反,您可以仅重新定义 Configuration Properties > Linker > General > Output File 并在该文件名中包含 $(ConfigurationName) 。

【讨论】:

  • 我检查了工作目录。它已经为 $(OutputPath) 设置。我确实将其更改为“所有配置”,但我仍然收到相同的错误消息。
  • 我以为你有一个 C++ 项目,其中 C++ 代码相当于 pyton 代码,但现在你似乎有一个实用程序或 ma​​kefile 项目调用 python?然后我不太记得 $(OutputPath) 通向哪里(我认为是可选目标的目录),但您可以在 python 代码中看到 os.getcwd() 是什么。
【解决方案2】:

你有没有想过这个?我遇到了完全相同的问题,而且我很确定这是完全相同的项目。

出于某种原因,Visual Studio 不会读取我的 .txt 文件。 .txt 文件是从用 python 编写的代码打开的,python 代码具有引用回用 C++ 编写的主程序的函数。对于关注这篇文章的其他人。

希望能尽快收到回复。 谢谢。 贾斯汀

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    相关资源
    最近更新 更多