市面上的Python教程基本都是以3.0以下版本来讲解的,python 从3.0之后一些语法都做了写更改,有时候可能会浪费比较多的时间,记录下使用过程中遇到的情况以备后查。
1、Print (1)需要加括号 (2)打印文件重定向
(1)print ('hello world!!')
(2) print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
log = open('test.txt','a') print (1,2,3, file=log, end='\n') print (4,5,6, file=log, end='\n') log.close() print (7,8,9)
2、commands modle 在3.0之后使用subprocess来代替。
Deprecated since version 2.6: The commands module has been removed in Python 3.0. Use the subprocess module instead.
>>> import commands >>> commands.getstatusoutput('ls /bin/ls') (0, '/bin/ls') >>> commands.getstatusoutput('cat /bin/junk') (256, 'cat: /bin/junk: No such file or directory') >>> commands.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') >>> commands.getoutput('ls /bin/ls') '/bin/ls' >>> commands.getstatus('/bin/ls') '-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
>>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') (0, '/bin/ls') >>> subprocess.getstatusoutput('cat /bin/junk') (256, 'cat: /bin/junk: No such file or directory') >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') >>> subprocess.getoutput('ls /bin/ls') '/bin/ls'
3. input 替代raw_input
raw_input Converts raw_input() to input().
while True: reply = input('Enter Text:') if reply == 'stop': break print (reply.upper())