我正在尝试在我的代码中实现一个基本的调试标志。我计划通过将 print 绑定到 logFile.write 来做到这一点,因此不是记录我的代码执行的过程,而是打印它,以便用户可以看到它完成并查看它可能会在哪里搞砸。我通过将 raw_input 绑定到输入以实现 python 2 和 python 3 的向后兼容性来执行相同的技术,但是当我尝试将 print 绑定到任何东西时,我得到一个语法错误。
在 Windows 上的 Python 3 上,该行正确运行,但在 UNIX 或 Windows 上的 python 2 上运行时,它会失败。
# If user has passed the 'debug' parameter then no log file will be created and instead will be printed in terminal
if (len(sys.argv) > 1 and sys.argv[1] == 'debug') or (len(sys.argv) > 2 and (sys.argv[1] == 'debug' or sys.argv[2] == 'debug')):
try:
logFile.write = print
except:
print('Debug is not working at this time')
这是我收到的错误
SyntaxError: invalid syntax
lax94t01> ./Automation.py userconfig.ini debug
File "./Automation.py", line 55
logFile.write = print
^