passing more than one variables to os.system in py

2020-06-16 05:39发布

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?

2条回答
太酷不给撩
2楼-- · 2020-06-16 06:15

This, for example, works:

os.system('%s %s' % ('ls', '-l'))
查看更多
不美不萌又怎样
3楼-- · 2020-06-16 06:36

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
查看更多
登录 后发表回答