exercise15 读取文件

来源:互联网 发布:其竟以此而殒其生乎 编辑:程序博客网 时间:2024/06/01 19:51

运行文件ex15.py

ex15_sample.txt  //记事本的内容

This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.


from sys import argv

script, filename=argv

txt=open(filename)

print "Here is your file %r:" %filename

print txt.read()

print "type the filename again:"
filename_again=raw_input(">")    # raw_input后面若为显示的内容,要加上引号!!!
txt_again=open(filename_again)

print txt_again.read()



$ python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.


Type the filename again:
>  ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.


最后需要用close()关闭文件

0 0
原创粉丝点击