【问题标题】:.bat file execution in python not giving the required results在python中执行.bat文件没有给出所需的结果
【发布时间】:2016-06-11 09:02:58
【问题描述】:

这是我的bat文件的内容

Proselint 是一个 exe 文件

proselint C:\Users\Rahul\Desktop\proselint-0.5.3\proselint\proselint\something.md > ooo.txt

双击运行bat文件会产生以下输出

C:\Users\Rahul\Desktop\proselint-0.5.3\proselint\proselint\something.md:2:1: misc.mondegreens 'a girl with kaleidoscope eyes' is the preferred form.
C:\Users\Rahul\Desktop\proselint-0.5.3\proselint\proselint\something.md:1:38: misc.mondegreens 'laid him on the green' is the preferred form.

ooo.txt 中的正确输出是什么 但是当我尝试从 python 脚本运行 bat 时,我得到以下输出

C:\Users\Rahul\Desktop\proselint-0.5.3\proselint\proselint>proselint C:\Users\Rahul\Desktop\proselint-0.5.3\proselint\proselint\something.md 1>ooo.txt

这只是要执行的命令 使用的python代码是:

import os
os.system("C:/Users/Rahul/Desktop/proselint-0.5.3/proselint/proselint/command.bat")

注意 :使用 SubProcess 也给了我与 ooo.txt 相同的输出

from subprocess import call
call("C:/Users/Rahul/Desktop/proselint-0.5.3/proselint/proselint/command.bat")

【问题讨论】:

  • 你能用与proselint不同的程序重现这个问题吗?在批处理文件中使用echo 或其他程序不会重现观察到的行为。
  • 使用 dir 命令效果很好。因此,使用 python 脚本运行时,只有 Windows 内置命令才能正常工作
  • proselint 的 windows 版本是从哪里得到的?
  • @Ashafix pip install proselint ?
  • 但它不会是 Python 模块而不是 exe 文件吗?

标签: python operating-system subprocess


【解决方案1】:

您的 .bat 文件必须在第一行包含 @echo off,例如:

@echo off

command parameter > ooo.txt

如果您不指定 @echooff,则将要执行的每一行都将打印到您的控制台。

我希望这能解决您的问题。

【讨论】:

  • Traceback(最近一次调用最后):文件“c:\anaconda3\lib\runpy.py”,第 170 行,在 run_module_as_main“main”,mod_spec ) 文件“c:\anaconda3\lib\runpy.py”,第 85 行,在 _run_code exec(code, run_globals) 文件“C:\Anaconda3\Scripts\proselint.exe__main_.py”,第 5 行,在 ImportError:无法导入名称'proselint'
  • 能否请您使用用于调用 proselint.exe 的实际代码编辑您的答案,因为问题似乎实际上并不是调用。
  • 我对您问题的复制完美无缺,所以很抱歉,但我无法帮助您。我高度怀疑该错误实际上不是由代码 sn-p 引起的。
猜你喜欢
  • 2015-03-04
  • 2013-10-14
  • 2021-05-26
  • 2020-04-04
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
相关资源
最近更新 更多