python读取单个文件操作

python读取单个文件,参考《笨方法学python》的第15节。

 

运行方式是采用:python python文件名 要读取的文件名

代码中

script, filename = argv

agrv是获取参数的,要理解sys.argv的用法。

 

读文件的代码示例:

 1 from sys import argv
 2 
 3 script, filename = argv
 4 
 5 txt = open(filename)
 6 print("Here's your file %r:" % filename)
 7 print(txt.read())
 8 
 9 print("Type the file again:")
10 file_again = input("> ")
11 
12 txt_again = open(file_again)
13 print(txt_again.read())

运行步骤:

 

posted @ 2018-04-24 18:06  打不过小怪兽leer  阅读(165)  评论(0编辑  收藏  举报