os.system not working, but typing the same thing into the command prompt works

懵懂的女人 提交于 2019-12-23 18:53:16

问题


I am trying to run python abaqus through the command prompt using

os.system('abaqus CAE noGUI=ODBMechens')

It doesn't seem to run anything, but if I go to the command prompt myself and type in

abaqus CAE noGUI=ODBMechens

it works. I am using python 2.7 on Windows 10. Thanks


回答1:


try using the subprocess module (it's newer) instead: for example,

subprocess.call(["ls", "-l"])

and in your example, it would be:

subprocess.call('abaqus CAE noGUI=ODBMechens')

More info on the difference between subprocess module and using os.system call:

The Difference between os.system and subprocess calls




回答2:


You should add before your code

import os
import subprocess
try:
    os.environ.pop('PYTHONIOENCODING')
except KeyError:
    pass

And then:

cmd = subprocess.Popen('abaqus CAE noGUI=ODBMechens',cwd=jobPath, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE, shell=True).communicate()[0]

Variable cmd contains your output. I found that this way it works.



来源:https://stackoverflow.com/questions/43037012/os-system-not-working-but-typing-the-same-thing-into-the-command-prompt-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!