python - 使用控制台启动程序时无法写入文件

标签 python file console

我最近开始使用 Python,我想创建一个程序来读取文件中的计算,执行它(使用 eval() 函数)并将结果写入另一个文件中。该程序必须通过控制台启动。

我已经创建了该程序,当我通过双击启动它时,它运行得很好。但是当我使用控制台启动程序时,它不会将结果写入文件中,并且我没有收到任何错误。我知道计算已经完成,因为结果写在控制台中。

我尝试过运行带有 .py 扩展名的程序,并使用 pyinstaller 将其编译为可执行文件。它们需要双击才能工作,但不能从控制台操作。

以下是我用来运行程序的命令:

F:\Path\To\App\calculator.exe

C:\Path\To\Python\python.exe F:\Path\To\App\calculator.py

我用来读取、评估和编写计算的代码

input = open('calcul.txt', 'r')
output = open('result.txt', 'w')

calcul = input.read()
print(calcul)
print(eval(calcul).toString())
output.write(eval(calcul).toFileString())

input.close()
output.close()

def toString(self):
        number = str (round(self.m_number, 4))
        number_scientific = str(format(self.m_number, ".3E"))
        imprecision = str (round(self.m_imprecision, 4))
        imprecision_scientific = str(format(self.m_imprecision, ".3E"))
        relative_imprecision = str(round(self.m_relative_imprecision * 100, 2))

        return "\t  Number \t\t= " + number + " \t= " + number_scientific + "\n\t  Imprecision \t\t= " + imprecision + " \t= " + imprecision_scientific + "\n\t  Relative Imprecision \t= " + relative_imprecision + "%\n\t"

def toFileString(self):
        return str (round(self.m_number, 4)) + '\n' + str (round(self.m_imprecision, 4))

当我以管理员身份运行控制台时,我有:

C:\WINDOWS\system32>F:\Users\Ludovic\Desktop\Apprentissage\C++\Qt\calculator\python_calculator\calculator.exe
Traceback (most recent call last):
  File "calculator.py", line 376, in <module>
    calcul = input.read()
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\WINDOWS\\system32\\calcul.txt'
[26580] Failed to execute script calculator

C:\WINDOWS\system32>C:\Users\Ludovic\AppData\Local\Programs\Python\Python38\python.exe F:\Users\Ludovic\Desktop\Apprentissage\C++\Qt\calculator\python_calculator\calculator.py
Traceback (most recent call last):
  File "F:\Users\Ludovic\Desktop\Apprentissage\C++\Qt\calculator\python_calculator\calculator.py", line 373, in <module>
    input = open('calcul.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'calcul.txt'

最佳答案

以管理员身份运行控制台

添加输入输出文件的路径

input = open('your_path_to_file\\calcul.txt', 'r') 
output = open('your_path_to_file\\result.txt', 'w')

或者将文件放入脚本文件夹中,然后像这样调用它们

import sys

input = open(sys.path[0]+'\\calcul.txt', 'r')
output = open(sys.path[0]+'\\result.txt', 'w')

更新

对于 .exe 和 .py 的通用文件路径,请尝试此操作(文件应位于 .exe 和 .py 文件夹中)

import sys

if getattr(sys, 'frozen', False):
    application_path = ''
else:
    application_path = sys.argv[0]+'\\'

input = open(application_path+'calcul.txt', 'r')
output = open(application_path+'result.txt', 'w')

关于python - 使用控制台启动程序时无法写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59722850/

相关文章:

firefox - Firefox 浏览器控制台没有提示?

python - YQLResponseMalformedError : Response malformed on yahoo_finance?

从动态分配的文件中计算最多重复出现的元音

Java 无法删除文件,因为它是在 Java SE BINARY 中打开的

java - 让 Selenium 等待文件下载完成

qt - Windows 控制台和 Qt Unicode 文本

python - 第 8 行 : 'crsf_token' , 上的无效 block 标记应为 'endblock'。您是否忘记注册或加载此标签?

python - 检查一个句子是否包含我列表中的两个或多个单词

Python Pandas 科学记数法不一致

在 C 中创建您自己的控制台图形库