习题 13-17 (解包,参数,文件读写等)
习题13
#--coding:utf-8--
#习题十三 参数、解包、变量
from sys import argv
script, first, second, third = argv # unpack
print ("The script is called:", script)
print ("Your first variable is:", first)
print ("Your second variable is:", second)
print ("Your third variable is:", third)
习题14
#--coding:utf-8--
#习题十四 提示和传递
from sys import argv
script, user_name = argv
prompt = '> '
print ("Hi %s, I'm the %s script." % (user_name, script))
print ("I'd like to ask you a few questions.")
print ("Do you like me %s ?" % user_name)
likes = input(prompt)
print ("Where do you live %s?" % user_name)
lives = input(prompt)
print ("What kind of computer do you have?")
computer = input(prompt)
print("""
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer))
习题15
# coding=utf-8
#习题15 读取文件
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 = input("> ")
txt_again = open(file_again)
txt_again.close()
print (txt_again.read())
习题16
#--coding:utf-8--
#习题16
from sys import argv
script,filename = argv
print ("We're going to erase %r." % filename)
print ("If you don't want that, hit CTRL-C (^C).")
print ("If you do want that, hit RETURN.")
input("?")
print ("Opening the file...")
target = open(filename, 'w') # 打开文件,写状态
print ("Truncating the file. Goodbye!")
target.truncate() #清空target文件
print ("Now I'm going to ask you for three lines.")
line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")
print ("I'm going to write these to the file.")
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print ("And finally, we close it.")
target.close() # 关闭文件
#readline 读取文本文件中的一行
16-1
#--coding:utf-8--
#习题16 附加2
#--coding:utf-8--
#习题16 附加2
from sys import argv
script,filename = argv
print ("We're going to erase %r." % filename)
print ("If you don't want that, hit CTRL-C (^C).")
print ("If you do want that, hit RETURN.")
input("?")
print ("Opening the file...")
target = open(filename, 'w') # 打开文件,写状态
print ("Truncating the file. Goodbye!")
target.truncate() #清空target文件
print ("Now I'm going to ask you for three lines.")
line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")
print ("I'm going to write these to the file.")
# noinspection PyStringFormat
target.wirte("%s\n$s\n#s\n" % (str(line1), str(line2), str(line3)))
#target.write(line1)
#target.write("\n")
#target.write(line2)
#target.write("\n")
#target.write(line3)
#target.write("\n")
print ("And finally, we close it.")
target.close() # 关闭文件
#readline 读取文本文件中的一行
16-2
#--coding:utf-8--
#习题16 附加2
from sys import argv
script,filename = argv
print("Open the file...")
target = open(filename)
print(target.read())
target.close()
习题17
#-*-coding:utf-8-*-
#习题17 更多文件操作
import os
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print("Copying from %s to %s" % (from_file, to_file))
#We could do these two on one line too, how?
input = open(from_file)
indata = input.read()
print("The input file is %d bytest long" % len(indata))
print("Does the output file exist? %r" % exists(to_file))
print("Ready, hit RETURN to continue, CTRL-C to abort.")
#input('>')
os.system("pause")
output = open(to_file, 'w')
output.write(indata)
print("Alright, all done.")
output.close()
input.close()
由于直接使用input()无法执行正常,所以import os,采用os.system("pause")进行处理。