《笨办法学python》源码 Ex15.py



# 命令行参数名为"ex_15_sample.txt"
txt文本内容如下:

--------------------------------------

This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
*************************************
I am huanghuan.
today is 2013-08-14.
I am very happy today.
what about you?

-------------------------------------

#-------------------------------------------------------------------------------
# Name:        ex_15.py
# Purpose:     读取txt文件.
#              open()    打开文件
#              read()    读取文件
#              close()   关闭文件.
# Author:      huanghuan
#
# Created:     14-08-2013
# Copyright:   (c) huanghuan 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------


from sys import argv

# 方式1.
# 命令行参数名为"ex_15_sample.txt"
script, file_name1 = argv

# 打开文本.
txt1 = open(file_name1)
print "here's your file %r:" % file_name1
# 调用read()读出文本内容.
print txt1.read()

txt1.close()

#---------------------------------------

"""
# 方式2.
print "Input the file name again:"
file_name2 = raw_input("> ")

# 打开文本.
txt2 = open(file_name2)
# 读入文本
print txt2.read()
"""

你可能感兴趣的:(《笨办法学python》源码 Ex15.py)