passing more than one variables to os.system in python

前端 未结 2 1628
感动是毒
感动是毒 2021-02-15 15:38

I want to pass two variables to the os.system() for example listing files in different format in specific directory like (ls -l testdirectory) in which both a switch and test di

相关标签:
2条回答
  • 2021-02-15 16:17

    you are asking about string formating (since os.system takes a string, not a list of arguments)

    cmd = "ls -{0} -{1}".format(var1,var2)
    #or cmd = "{0} -{1} -{2}".format("ls","l","a")
    os.system(cmd)
    

    or

    cmd = "ls -%s -%s"%(var1,var2)
    

    or

    cmd = "ls -"+var1+" -"+var2
    
    0 讨论(0)
  • 2021-02-15 16:30

    This, for example, works:

    os.system('%s %s' % ('ls', '-l'))
    
    0 讨论(0)
提交回复
热议问题