我如何在Python中命名一个外部命令,就好像我把它写在一个<unk>或命令中?


当前回答

作为一个例子(在Linux):

import subprocess
subprocess.run('mkdir test.dir', shell=True)

这在当前目录中创建 test.dir. 请注意,这也是有效的:

import subprocess
subprocess.call('mkdir test.dir', shell=True)

使用 os.system 的同等代码是:

import os
os.system('mkdir test.dir')

最好的做法是使用子过程而不是OS,与.run 受欢迎的.call. 所有你需要知道的子过程在这里. 此外,请注意,所有的 Python 文档都可以从这里下载. 我下载了 PDF 包装为.zip. 我提到这一点,因为有一个好概述的OS 模块在 tutorial.pdf (页面 81 ) 。 此外,它是一个授权的资源的 Python 编码器。

其他回答

只需添加到讨论中,如果您使用 Python 控制台,您可以从 IPython 呼叫外部命令,而在 IPython 快速时,您可以通过预定“!”。您也可以将 Python 代码与 Shell 结合起来,并将 Shell 脚本的输出分配给 Python 变量。

例如:

In [9]: mylist = !ls

In [10]: mylist
Out[10]:
['file1',
 'file2',
 'file3',]
import subprocess

p = subprocess.run(["ls", "-ltr"], capture_output=True)
print(p.stdout.decode(), p.stderr.decode())

网上尝试

经过一些研究,我有下面的代码,这对我来说非常好,它基本上在实时打印了标准输出和标准错误。

stdout_result = 1
stderr_result = 1


def stdout_thread(pipe):
    global stdout_result
    while True:
        out = pipe.stdout.read(1)
        stdout_result = pipe.poll()
        if out == '' and stdout_result is not None:
            break

        if out != '':
            sys.stdout.write(out)
            sys.stdout.flush()


def stderr_thread(pipe):
    global stderr_result
    while True:
        err = pipe.stderr.read(1)
        stderr_result = pipe.poll()
        if err == '' and stderr_result is not None:
            break

        if err != '':
            sys.stdout.write(err)
            sys.stdout.flush()


def exec_command(command, cwd=None):
    if cwd is not None:
        print '[' + ' '.join(command) + '] in ' + cwd
    else:
        print '[' + ' '.join(command) + ']'

    p = subprocess.Popen(
        command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd
    )

    out_thread = threading.Thread(name='stdout_thread', target=stdout_thread, args=(p,))
    err_thread = threading.Thread(name='stderr_thread', target=stderr_thread, args=(p,))

    err_thread.start()
    out_thread.start()

    out_thread.join()
    err_thread.join()

    return stdout_result + stderr_result

作为一个例子(在Linux):

import subprocess
subprocess.run('mkdir test.dir', shell=True)

这在当前目录中创建 test.dir. 请注意,这也是有效的:

import subprocess
subprocess.call('mkdir test.dir', shell=True)

使用 os.system 的同等代码是:

import os
os.system('mkdir test.dir')

最好的做法是使用子过程而不是OS,与.run 受欢迎的.call. 所有你需要知道的子过程在这里. 此外,请注意,所有的 Python 文档都可以从这里下载. 我下载了 PDF 包装为.zip. 我提到这一点,因为有一个好概述的OS 模块在 tutorial.pdf (页面 81 ) 。 此外,它是一个授权的资源的 Python 编码器。

从 OpenStack Neutron 中获取网络 ID:

#!/usr/bin/python
import os
netid = "nova net-list | awk '/ External / { print $2 }'"
temp = os.popen(netid).read()  /* Here temp also contains new line (\n) */
networkId = temp.rstrip()
print(networkId)

新网列表

+--------------------------------------+------------+------+
| ID                                   | Label      | CIDR |
+--------------------------------------+------------+------+
| 431c9014-5b5d-4b51-a357-66020ffbb123 | test1      | None |
| 27a74fcd-37c0-4789-9414-9531b7e3f126 | External   | None |
| 5a2712e9-70dc-4b0e-9281-17e02f4684c9 | management | None |
| 7aa697f5-0e60-4c15-b4cc-9cb659698512 | Internal   | None |
+--------------------------------------+------------+------+

印刷输出(networkId)

27a74fcd-37c0-4789-9414-9531b7e3f126