passing more than one variables to os.system in python

為{幸葍}努か 提交于 2020-01-01 05:37:04

问题


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 directory are variable. I know for single variable this one works:

option=l os.sytem('ls -%s' option)

but I dont know how to pass two variables?


回答1:


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



回答2:


This, for example, works:

os.system('%s %s' % ('ls', '-l'))


来源:https://stackoverflow.com/questions/22101931/passing-more-than-one-variables-to-os-system-in-python

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