【问题标题】:I get a value of 1 from the os.system(). Is it good? Has the .bat file opened correctly?我从 os.system() 得到值 1。好吗? .bat 文件是否正确打开?
【发布时间】:2017-07-21 11:10:32
【问题描述】:

我在 python 中运行了以下命令。

retval = os.system(..Path_to_my_exe_file); I get an retval of 1
print retval

无论如何要打印在python代码中打开的exe文件的内容。 我对此比较陌生,如果有任何帮助,我将不胜感激。

【问题讨论】:

  • “exe文件的内容”是什么意思?
  • @我在 Windows 上工作。我想从 bash 文件中读取内容并在我的 python 代码中打印出来。我的 retval 为 1。文件是否正确打开?
  • 现在有 bash 文件了吗?那是从哪里来的?你有什么,你想用它做什么,你想看到什么?
  • 我正在做一个个人项目,我需要一些帮助。我想创建一个 python 脚本,它将查询 data_values.bat 的内容并以以下格式打印查询结果。格式: .bat 位于另一个文件夹,python 代码位于桌面。
  • with open('..Path_to_my_exe_file', 'r') as rf: print(rf.read())。如果我理解正确的话。如果您想读取文本文件的内容,为什么要尝试执行它?

标签: python python-2.7


【解决方案1】:

os.system() 返回的值的含义取决于您使用的 shell。请参阅https://docs.python.org/2/library/os.html#os.system 了解更多信息。

然而,从 cmets 来看,您似乎正在寻找不同的东西。

要在 python 中运行批处理文件,并捕获批处理文件的输出,您可以使用以下命令(来自:Piping Batch File output to a Python script

import subprocess

output= subprocess.Popen(
    ("Path to your batch file", "an argument", "another argument"),
    stdout=subprocess.PIPE).stdout

for line in output:
    # do your work here

output.close()

要读取批处理文件中的代码,您可以这样做:

batch_code = open("Path to your batch file",'r').read()

【讨论】:

    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多