关闭

exercise15 读取文件

38人阅读 评论(0) 收藏 举报

运行文件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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3041次
    • 积分:387
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档