【发布时间】:2017-07-31 13:15:54
【问题描述】:
我有一个程序需要向/tmp 目录写入一个小 bash 脚本,以提示输入凭据:
linux_prompt_script = (
'printf "Proxy authentication failed.\n"'
'\nread -s -p "Enter Password to try again: " mypassword'
'\nprintf "Proxy authentication succeeded\n'
)
当我现在写它时,cat'ed 时看起来像这样:
printf "Proxy authentication failed.
"
read -s -p "Enter Password to try again: " mypassword
printf "Proxy authentication succeeded
这显然行不通。有没有办法可以在不创建新行的情况下写一个换行符\n,也可以写它来创建一个新行?
到目前为止我所拥有的:
linux_prompt_script = (
'printf "Proxy authentication failed.\n"'
'\nread -s -p "Enter Password to try again: " mypassword'
'\nprintf "Proxy authentication succeeded\n'
)
def _prompt_linux():
def _rand_filename(chars=string.ascii_letters):
retval = set()
for _ in range(0, 6):
retval.add(random.choice(chars))
return ''.join(list(retval))
filename = _rand_filename()
filepath = "/tmp/{}.sh".format(filename)
while True:
with open(filepath, "a+") as sh:
sh.write(linux_prompt_script)
【问题讨论】:
-
只需像
\\n一样转义反斜杠
标签: python python-2.7 newline