《“笨办法”学python3》Ex 15

知识点:

读取文件. 脚本读取和命令行读取. 

txt = open(filename)
print(txt.read())

file_again = input("> ")
txt_again = open(file_again)
print(txt_again.read())

程序:

from sys import argv

script, filename = argv

txt = open(filename)

print(f"Here's your file {filename}: ")
print(txt.read())

print("Type the filename again: ")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read())

输出:

PS C:\Users\xue weiruan\github> python ex15.py ex15_sample.txt
Here's your file ex15_sample.txt:
Who cares?
The code is fun.
Enjoy it.
Type the filename again:
> ex15_sample.txt
Who cares?
The code is fun.
Enjoy it.
PS C:\Users\xue weiruan\github>

 

你可能感兴趣的:(Python)