【问题标题】:Launching shell command through python通过python启动shell命令
【发布时间】:2019-06-12 09:59:51
【问题描述】:

我正在尝试为文件列表执行命令外壳,所以我尝试了这段代码

import glob
import shutil
import os


def recursListPath(path, nth = 1):
list = []
for i in range (1, nth+1):
    currentpath = path + (i * "/*")
    for name in glob.glob(currentpath):
        list.append(name)
return list

version = "SPIRIT/1685-2009"
DirPath = "D:\\all-data"
list = recursListPath(DirPath, 4)
for item in list:
   if os.path.isfile(item):
      fichier=open(item, encoding="mbcs")
      for ligne in fichier :
        if version in ligne:
           print("'%s' 2009" % fichier.name)
           os.system("'%s' start C:\> script.sh -file textVersionChange.tcl -w wk -log convertversion.log -args" %fichier.name "converteddir" )
      fichier.close()

为每个文件启动控制台,但没有生成任何内容 如何在 os.system 的命令中间使用变量?

【问题讨论】:

  • 请修正您的代码缩进。

标签: python-3.x os.system


【解决方案1】:

这不是真正的os.system 问题。 我会先说我不在 Windows 上工作,我假设你的实际命令行是正确的? (C:\> 在我看来有点奇怪)

我不得不说我实际上不确定这是怎么回事: "'%s' start C:\> script.sh -file textVersionChange.tcl -w wk -log convertversion.log -args" %fichier.name "converteddir" 没有返回语法错误。

我不确定您希望该命令是什么样子,但您可以更改字符串格式以开始。

试试:

"'{name}' start C:\> script.sh -file textVersionChange.tcl -w wk -log convertversion.log -args converteddir".format(name=fichier.name)

另外,我会考虑使用较新的subprocess module 而不是os.system。

然后您可以将命令行作为列表提供:

[fichier.name, "start", "C:\> script.sh", "-file", "textVersionChange.tcl", "-w", "wk", "-log", "convertversion.log", "-args", fichier.name, "converteddir"]

其中可以包含变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多