首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 3版本较之前版本语法的一些

Python 3版本较之前版本语法的一些

作者头像
py3study
发布2020-01-03 12:06:49
6250
发布2020-01-03 12:06:49
举报
文章被收录于专栏:python3python3

市面上的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()) 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档