'How to pass a string with special chars in python to os.system. (E.g. python test.py |& tee output.txt)

The command will fail as a result of char "&". E.g. python test.py |& tee output.txt



Solution 1:[1]

On my system, using |& gives me this error message

sh: -c: line 0: syntax error near unexpected token `&'

|& is not a syntax supported by sh, but it is supported by bash. This isn't an issue with "special characters", but rather an issue with the syntax supported by the shell that os.system() runs. If this is the error you're getting, you could use this sh-compatible syntax instead

os.system("python test.py 2>&1 | tee output.txt")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rchome