python 文件

在python命令行中打开文件为

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

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

 1 >>> txt = open('15_sample.txt')
 2 >>> print txt.read()
 3 this is stuff I typed into a file
 4 It's 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 It's really cool stuff
18 Lots and lots of fun to have in here

在.py的文件中

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

  两种打开方式

打开文件后记得用

txt.close()关闭文件。

posted on 2016-12-12 09:22  zhuzhu2016  阅读(213)  评论(0编辑  收藏  举报

导航