运行文件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.
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()关闭文件
|
|