python2与python3语法区别之_重定向

python2与python3两版本的区别是众所周知的,今天主要记录python下版本2 与版本3的区别

  • python2
In [7]: logfile  = open('/tmp/mylog.log', 'a')

In [8]: print >> logfile, 'writeen by python version 2'

In [9]: logfile.close()

In [10]: cat /tmp/mylog.log
writeen by python version 2
  • python3
In [1]: help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='
', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
⇒  ipython
Python 3.5.1 (default, Jan 23 2017, 08:19:40)
Type "copyright", "credits" or "license" for more information.

IPython 5.2.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import sys

In [2]: f = open('/tmp/mylog.log', 'w')

In [3]: print('writeen by python version 2', file=f)

In [4]: f.close()

In [5]: cat /tmp/mylog.log
writeen by python version 2
writeen by python version 3
原文地址:https://www.cnblogs.com/ZhangRuoXu/p/6388918.html