简体   繁体   English

如何将 python 中带有特殊字符的字符串传递给 os.system。 (例如 python test.py |& tee output.txt)

[英]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 "&".该命令将因字符“&”而失败。 Eg python test.py |& tee output.txt例如 python test.py |& tee output.txt

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 . |&不是sh支持的语法,但bash支持它。 This isn't an issue with "special characters", but rather an issue with the syntax supported by the shell that os.system() runs.这不是“特殊字符”的问题,而是os.system()运行的 shell 支持的语法问题。 If this is the error you're getting, you could use this sh -compatible syntax instead如果这是您遇到的错误,则可以改用此sh兼容语法

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

暂无
暂无

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

相关问题 使用 python 排列 output.txt? - Arrange output.txt using python? Python os.getcwd()在路径中返回波浪号。 例如C:\\ MYFOLD~1 \\ test - Python os.getcwd() returns with tilde in the path. e.g. C:\MYFOLD~1\test 和弦字典(Python)中特殊字符(例如#、/)的正则表达式问题 - Regex problems with special characters (e.g. #, /) in a chord dictionary (Python) 如何使用 Anaconda 在后台运行 python 脚本? ('nohup python -u test.py &' 不起作用!) - How to run python script in background using Anaconda? ('nohup python -u test.py &' doesn't work!) 在python中调用fortran代码时如何处理全局变量(例如,使用f2py)? - How to deal with global variables when calling fortran code in python (e.g. with f2py)? Python:为文件设置多个属性(例如系统、隐藏) - Python: Setting Multiple Attributes for a file (e.g. System, Hidden) 如何在 Python3 中将特殊参数传递给 os.system? - How to pass special parameter into os.system in Python3? '%%file test.py' 在 python 中是什么意思? - what does '%%file test.py' mean in python? 如何将任意 Python 对象(例如 InMemoryUploadFile)传递给不同的 Django 视图 - How to pass arbitrary Python object (e.g. InMemoryUploadFile) to a different Django view 如何启动后台 shell 或内核(例如 python)、连接到它并检索输出 - How to start a background shell or kernel (e.g. python), connect to it, and retrieve output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM