Python运行OS命令,里面有引号

时间:2013-08-23 19:15:57

标签: python-2.7 operating-system

#!/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')

我收到以下错误...

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

然后在我解决错误后,我想用机器名变量替换IP。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你应该逃避你的反斜杠。将\\替换为\\\\

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')

或者,通过添加raw前缀:

来制作字符串r
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')

另见: