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来传递文件路径和名称