Python3 在 os.popen() 语句中使用来自 Python 的变量 (WINDOWS)
Python3 Using Variables from Python in os.popen() statement (WINDOWS)
我站在下面的问题面前。
我正在使用 python3.
构建一个小型卸载 Java 工具
我可以读出我需要的所有内容,但是当我尝试将卸载命令发送到 Windows 时,我无法正确完成解析。
搜索已安装软件的命令如下所示:
wmic_output = os.popen('''wmic product where "name like 'Java 8 Update %%'" get name''').read()
os.popen 字符串中没有任何变量,一切正常。现在尝试执行命令似乎更棘手...
productname = str(uninstallcall[inx])
# Check if Version is installed, if so uninstall
wmic_output1 = os.popen('''wmic product where "name like '%s'" get name''').read()
result1 = parse_wmic_output(wmic_output1 % (productname))
是的,productname 变量在这样做时打印得很好:/
使用此处找到的以下片段解析 Im:http://autosqa.com/2016/03/18/how-to-parse-wmic-output-with-python/:
def parse_wmic_output(text):
result = []
# remove empty lines
lines = [s for s in text.splitlines() if s.strip()]
# No Instance(s) Available
if len(lines) == 0:
return result
header_line = lines[0]
# Find headers and their positions
headers = re.findall('\S+\s+|\S$', header_line)
pos = [0]
for header in headers:
pos.append(pos[-1] + len(header))
for i in range(len(headers)):
headers[i] = headers[i].strip()
# Parse each entries
for r in range(1, len(lines)):
row = {}
for i in range(len(pos)-1):
row[headers[i]] = lines[r][pos[i]:pos[i+1]].strip()
result.append(row)
return result
%s
, are you trying to insert a variable into that position? if so
you need to do "some %s variable" % variable_to_inser
. Or a cleaner
way, do f"some {productname} here"
(f being important at the start
of the string). – @Torxed
这解决了我的问题如下:
wmic_output = os.popen(f'''wmic product where "name like {variablename}" get name''').read()
我站在下面的问题面前。 我正在使用 python3.
构建一个小型卸载 Java 工具我可以读出我需要的所有内容,但是当我尝试将卸载命令发送到 Windows 时,我无法正确完成解析。
搜索已安装软件的命令如下所示:
wmic_output = os.popen('''wmic product where "name like 'Java 8 Update %%'" get name''').read()
os.popen 字符串中没有任何变量,一切正常。现在尝试执行命令似乎更棘手...
productname = str(uninstallcall[inx])
# Check if Version is installed, if so uninstall
wmic_output1 = os.popen('''wmic product where "name like '%s'" get name''').read()
result1 = parse_wmic_output(wmic_output1 % (productname))
是的,productname 变量在这样做时打印得很好:/
使用此处找到的以下片段解析 Im:http://autosqa.com/2016/03/18/how-to-parse-wmic-output-with-python/:
def parse_wmic_output(text):
result = []
# remove empty lines
lines = [s for s in text.splitlines() if s.strip()]
# No Instance(s) Available
if len(lines) == 0:
return result
header_line = lines[0]
# Find headers and their positions
headers = re.findall('\S+\s+|\S$', header_line)
pos = [0]
for header in headers:
pos.append(pos[-1] + len(header))
for i in range(len(headers)):
headers[i] = headers[i].strip()
# Parse each entries
for r in range(1, len(lines)):
row = {}
for i in range(len(pos)-1):
row[headers[i]] = lines[r][pos[i]:pos[i+1]].strip()
result.append(row)
return result
%s
, are you trying to insert a variable into that position? if so you need to do"some %s variable" % variable_to_inser
. Or a cleaner way, dof"some {productname} here"
(f being important at the start of the string). – @Torxed
这解决了我的问题如下:
wmic_output = os.popen(f'''wmic product where "name like {variablename}" get name''').read()