简体   繁体   English

如何使用python 2在os.system命令中转义/使用单引号?

[英]How do I escape/use single quotes in os.system command with python 2?

i have built a small script that runs a simple shell utility called imapsync, with a bunch of variables taken from a dictionary, the command is as follows: 我建立了一个小脚本,运行一个名为imapsync的简单外壳实用程序,并从字典中获取一堆变量,命令如下:

os.system("imapsync --host1 %s --user1 %s --password1 '%s' --host2 %s --user2 %s --password2 '%s' --ssl1 --no-modulesversion --ssl2" % (fromHost, emails, passwords, toHost, emails, passwords))

the deal is that passwords often contain special characters, example: djDJS*^%%%^&) 关键是密码通常包含特殊字符,例如:djDJS * ^ %%% ^&)

this imapsync tool allows such characters if enclosed in single quotes: 'djDJS*^%%%^&)' 如果用单引号引起来,则此imapsync工具允许使用此类字符:'djDJS * ^ %%% ^&)'

what I am trying to achieve is post the single quotes in the command, itself.. I tried "'", backquotes - ``, escaped quotes - \\'\\', enclosing the command in single quotes, nothing worked thus far 我想要实现的是在命令本身中发布单引号。.我尝试了“'”,反引号-,转义引号-\\'\\',将命令括在单引号中,到目前为止没有任何效果

After looking through the documentation of imapsync, I found the recommendation to enclose passwords in double quotes within single quotes to avoid common problems . 浏览imapsync文档后,我发现建议将密码用双引号括在单引号内,以避免常见问题

Since you already start the string with double quotes, you have to escape the double quotes around your password with a backslash \\" . 由于您已经用双引号将字符串开头,因此必须用反斜杠\\"来对密码周围的双引号进行转义。

There are also two things you could do to make your code even better. 您还可以做两件事来使您的代码变得更好。 First, you can use .format syntax for string formatting instead of the old % syntax. 首先,可以将.format语法用于字符串格式化,而不是旧的%语法。

Second replace os.system with subprocess.Popen . 第二替换os.systemsubprocess.Popen This allows you to split your command string into a list of all arguments, which looks more clear. 这使您可以将命令字符串拆分为所有参数的列表,这看起来更加清晰。

Your new code would look like 您的新代码看起来像

import subprocess

args = [
  "imapsync",
  "--host1",
  fromHost,
  "--user1",
  emails,
  "--password1",
  "'\"{}\"'".format(passwords),
  "--host2",
  toHost,
  "--user2",
  emails,
  "--password2",
  "'\"{}\"'".format(passwords),
  "--ssl1",
  "--no-modulesversion",
  "--ssl2"
]

p = subprocess.Popen(args, stdout=subprocess.PIPE)

output = p.communicate()[0]

print(output)

In this example Popen.communicate is used to gather the output of the imapsync command as a string. 在这个例子中Popen.communicate用于收集所述imapsync命令的输出为字符串。 The communicate method returns a tuple with the outputs of the subprocess to stdout and stderr streams. communicate方法将带有子进程输出的元组返回到stdoutstderr流。

If you also want to read the output to stderr from the subprocess, change the code as following: 如果您还想从子流程读取输出到stderr ,请按如下所示更改代码:

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

output, errors = p.communicate()

print(output)
print(errors)

The best format for passing string parameters in Python is to use the format string method. 在Python中传递字符串参数的最佳格式是使用格式字符串方法。 You could do something like this: 您可以执行以下操作:

line_command = "imapsync --host1 {fromHost} --user1 {emails} --password1 '\"{passwords}\"' --host2 {toHost} --user2 {emails} --password2 '\"{passwords}\"' --ssl1 --no-modulesversion --ssl2".format(fromHost=fromHost, emails=emails, passwords=passwords, toHost=toHost)
os.system(line_command)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM