Visual Studio C++和Python,使用输入文件,FileNotFoundError [Errno 2]没有此类文件或目录

fdbelqdn  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(257)

我正在使用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++项目的release文件夹中。有人能告诉我为什么会出现这个错误吗?
下面是我的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()
ffscu2ro

ffscu2ro1#

默认情况下,Visual Studio在.vcxproj* 文件所在的项目目录中启动C++程序,而不是在生成可执行文件的目录中启动a a a程序。可以在项目属性中的“配置属性”〉“调试”〉“工作目录”下配置该目录。在您的情况下,请将其设置为**$(OutputPath)**。确保选择对话框顶部的“所有配置”(以及所有平台,如果有多个)。此设置存储在.vcxproj.user文件中。
或者,您也可以通过更改“配置属性”〉“常规”〉“输出目录”,在保存输入文件的目录中生成可执行文件。但这样做会有混淆调试和发布配置中的工件的风险。相反,您可以只重新定义“配置属性”〉“链接器”〉“常规”〉“输出文件”,并在该文件名中包含$(ConfigurationName)。

相关问题