【发布时间】: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