【问题标题】:Python: How to get the string only in the commandPython:如何仅在命令中获取字符串
【发布时间】:2020-01-02 13:36:31
【问题描述】:

由于某种原因,命令的字符串给了我内存引用,而不是我在这个例子中寻找的简单的“pwd”

Command is (<tests/olv/olv/install/auto_ovirt_st_setup.InstallTest instance at 0xec118b4c>, 'pwd')

记录代码

def run_command_checking_exit_code(*command):
    """ Runs a command"""
    Log.info("Command is " + str(command))

def install_lago(self):
    command = ["ls", "-l"]
    self.run_command_checking_exit_code('pwd')
    """run the conductor profiles required to install OLVM """
    #Log.test_objective('TODO')
    #run_command_checking_exit_code('ls -al')
    """
    yum_list = ["epel-release", "centos-release-qemu-ev", "python-devel", "libvirt", "libvirt-devel", "libguestfs-tools", "libguestfs-devel", "gcc", "libffi-devel", "openssl-devel", "qemu-kvm-ev"]
    for yum in yum_list

    ret, msg, tup = self.client.run('/qa/conductor/tests/' + OSSE_OLV_VERSION + '/installer/installerfactory.py -s ' + OSSE_OLV_ENGINE_HOST + ' -t OS_OL7U6_X86_64_PVHVM_30GB -c 10.1.0.10 -o ' + self.log_jobdir_cc +'/vm_install_ol7.6', timeout=1000000)
    if ret:
        self.tc_fail('Creation of OLV Engine VM failed')
    ret, msg, tup = self.client.run('/qa/conductor/tests/' + OSSE_OLV_VERSION + '/installer/installerfactory.py -s ' + OSSE_OLV_ENGINE_HOST +' -p ovirt-engine -c 10.1.0.10 -o ' + self.log_jobdir_cc + '/engine_deploy', timeout=1000000)
    if ret:
        self.tc_fail('Install of OLV Engine Host failed')
    self.tc_pass('OLV Engine Host installed')
    """

def main(self):
    self.install_lago()

def __init__(self):
    self.main()

【问题讨论】:

  • command 在这里是一个元组,试试str(command[0])' '.join([str(item) for item in command])

标签: python pexpect


【解决方案1】:

假设 run_command_checking_exit_code 与其他方法属于同一类,它的 arg 列表中应该有 self:def run_command_checking_exit_code(self, command)

您不需要在方法定义中使用*,您只是将一个字符串传递给该方法,并且应该能够通过使用command 来打印该字符串。

其他几件事:

1) 这里的 main() 方法并不是真正需要的,你不应该有一个函数,它的唯一代码行是调用另一个函数。

2) 如果您想用列表而不是字符串调用run_command_checking_exit_code(例如将command = ['ls', '-l'] 列表传递给它,那么您需要研究如何在Python 中打印列表。一个简单的谷歌搜索会告诉您有几种方法可以做到这一点。

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    相关资源
    最近更新 更多