Python 官方文档:入门教程 => 点击学习
市面上的python教程基本都是以3.0以下版本来讲解的,Python 从3.0之后一些语法都做了写更改,有时候可能会浪费比较多的时间,记录下使用过程中遇到的情况以备后查。 1、Print (1)需要加括号 (2)打印文件重定向 (1)pr
市面上的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())
--结束END--
本文标题: Python 3版本较之前版本语法的一些
本文链接: https://www.lsjlt.com/news/191842.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0