您的位置:首页 > 编程语言 > Python开发

【Python】Learn Python the hard way, ex15 读取文件

2015-10-07 15:09 671 查看
from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()
txt.close()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()
txt_again.close()
"""
Test Result:

bogon:~ myRMBP$ python /Users/myRMBP/Desktop/ex15.py /Users/myRMBP/Desktop/ex15_sample.txt
Here's your file '/Users/myRMBP/Desktop/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:
> /Users/myRMBP/Desktop/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.

"""


心得:

通过argv来传递文件路径和名称
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: