Python读取文件

来源:互联网 发布:oa 办公系统java实现 编辑:程序博客网 时间:2024/05/06 01:24
from sys import argvscript,filename = argvtxt = open(filename)//从命令行参数获取文件名print("Here's your file %r:" %filename)#%rprint(txt.read())print("I'll also ask you to type it again:")file_again = input(">")#二次读取,从程序内部输入文件名txt_again = open(file_again)print(txt_again.read())

从上篇文章,学过了命令行的使用方法,上段代码中从命令行中获取到文件的名字,通过open("filename")来打开文件,txt.read()方法来读取文件中的内容。

0 0