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='\n', 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM