4 ms·
Pipeline in Python w/o invoking the shell: username = input("Hello, what's your name? ") p1 = Popen(["figlet", f"Welcome, {username}"], stdout=PIPE)
by js2 2y ago
Pipeline in Python w/o invoking the shell:
username = input("Hello, what's your name? ")
p1 = Popen(["figlet", f"Welcome, {username}"], stdout=PIPE)
p2 = Popen(["lolcat", "-f"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
banner = p2.communicate()[0]
print(banner)
https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline https://docs.python.org/3/library/subprocess.html#replacing-...
Don't use the shell unless you absolutely have to, and when you do have to, use shlex.quote for quoting arguments:
https://docs.python.org/3/library/shlex.html#shlex.quote https://docs.python.org/3/library/shlex.html#shlex.quote
username = input("Hello, what's your name? ")
banner = check_output(f"figlet "Welcome, {quote(username)}" | lolcat -f", shell=True)
print(banner)
For something this simple, you could also just use `subprocess.check_output` twice:
username = input("Hello, what's your name? ")
banner = check_output(["figlet", f"Welcome, {username}"])
banner = check_output(["lolcat", "-f"], input=banner)
print(banner)