1 # 从python的内置模块中引入模组 2 from sys import argv 3 4 # 解包,获取文件名 5 script, filename = argv 6 7 #返回某文件对象 8 txt = open(filename) 9 10 #打印这句话,嵌入文件名 11 print(f"Here's your file {filename}:") 12 #读取文件内容并且打印 13 print(txt.read()) 14 15 print("Type the filename again:") 16 # 把用户输入提示符号设置为>,赋值给file_again 17 file1 = input(">") 18 19 #打开文件 20 txt_again = open(file1) 21 22 #打开文件并且打印 23 print(txt_again.read())
用input写这个脚本
1 filename = input("输入文件名字:") 2 3 txt = open(filename) 4 5 print(f"Here's your file {filename}:") 6 print(txt.read()) 7 8 print("Type the filename again:") 9 file_again = input(">>>") 10 txt_again = open(file_again) 11 12 print(txt_again.read())
在提示符下用open打开一个文件