简体   繁体   English

写入日志文件时 Python 3 和 Python 2 之间的语法更改

[英]Syntax changes between Python 3 and Python 2 when writing to a log file

My job currently utilizes python 2.7 for scripting.我的工作目前使用 python 2.7 进行脚本编写。 However, I'm currently working on script with python 3.7.但是,我目前正在使用 python 3.7 编写脚本。 Normally, when we need to write to a log file the code looks like this:通常,当我们需要写入日志文件时,代码如下所示:

log = file(r'\\server\blah\result.log', 'w')

print >>log, 'BEGIN DAILY REPORT ' + strftime("%m/%d/%y %H:%M:%S", localtime()) + '\n

However, in 3.7 I'm getting this error: "NameError: name 'file' is not defined".但是,在 3.7 中,我收到此错误:“NameError: name 'file' is not defined”。 Is this the result of syntax changes?这是语法变化的结果吗? Forgive me as I'm new to python.请原谅我,因为我是 python 的新手。

To open a file in Python 3.x, you need to use open(filename, mode) .要在 Python 3.x 中打开文件,您需要使用open(filename, mode)

Look here for more information and examples. 在此处查看更多信息和示例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM