Python 2.7 Python运行OS命令,内部带有引号

Python 2.7 Python运行OS命令,内部带有引号,python-2.7,operating-system,Python 2.7,Operating System,我收到以下错误 myhostname:~./getLocalAdminNames.py sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串 然后在解决错误后,我想用machineName变量替换IP 任何帮助都将不胜感激。你应该避开你的责难。将\\替换为\\\: 或者,通过添加r前缀使字符串成为原始字符串: 另见: 尝试打印发送到os.system的字符串,以了解报价是

我收到以下错误

myhostname:~./getLocalAdminNames.py sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串 sh:语法错误:未终止的引号字符串

然后在解决错误后,我想用machineName变量替换IP


任何帮助都将不胜感激。

你应该避开你的责难。将\\替换为\\\:

或者,通过添加r前缀使字符串成为原始字符串:

另见:


尝试打印发送到os.system的字符串,以了解报价是否正确。或者更好,只需运行wmic,在Python中执行其余的grep和cut,这将使您的生活更加轻松。谢谢!这是可行的,但现在该变量中的每台机器都将是一个主机名。我似乎在使用以下命令时遇到问题。。。os.systemr'wmic-U corp.fakedomain.com/domainuser%domainpass/'+machineName+'从Win32_用户帐户中选择*,其中LocalAccount=True | grep 500 | cut-d\\-f2 | cut-d |-f1'
#!/usr/bin/python
import os

readLine = open('desktops.txt','r')

for line in readLine:
        machineName = line
        query = os.system('wmic -U corp.fakedomain.com/domainusername%password //192.168.1.100 "Select * from Win32_UserAccount Where LocalAccount = True"|grep "500|"|cut -d "\\" -f 2|cut -d "|" -f1')
os.system('wmic -U corp.fakedomain.com/domainusername%password //192.168.1.100 "Select * from Win32_UserAccount Where LocalAccount = True"|grep "500|"|cut -d "\\\\" -f 2|cut -d "|" -f1')
os.system(r'wmic -U corp.fakedomain.com/domainusername%password //192.168.1.100 "Select * from Win32_UserAccount Where LocalAccount = True"|grep "500|"|cut -d "\\" -f 2|cut -d "|" -f1')