首页 > 编程语言 > 详细

python 文件

时间:2016-12-12 09:51:41      阅读:238      评论:0      收藏:0      [点我收藏+]

在python命令行中打开文件为

  注意命令行要加单引号,否则会报错

  命令行的2种读/打开方式

 1 >>> txt = open(15_sample.txt)
 2 >>> print txt.read()
 3 this is stuff I typed into a file
 4 Its really cool stuff
 5 Lots and lots of fun to have in here
 6 
 7 >>> txt.close()
 8 >>> txt = open(15_sample.txt)
 9   File "<stdin>", line 1
10     txt = open(15_sample.txt)
11                        ^
12 SyntaxError: invalid syntax
13 >>> 
14 >>> 
15 >>> print(open(15_sample.txt).read())
16 this is stuff I typed into a file
17 Its really cool stuff
18 Lots and lots of fun to have in here

在.py的文件中

  在文件中可以看到open不需要加单引号,因为argv读入的输入文件是以字符串形式读入。

  两种打开方式

技术分享

打开文件后记得用

txt.close()关闭文件。

python 文件

原文:http://www.cnblogs.com/zhuzhu2016/p/6163228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!