首页 > 代码库 > python 文件

python 文件

在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 文件