一,作业内容
笨办法学Python习题13-17
二,作业代码
习题13 参数、解包和变量
from sys import argv
script, first, second, third = argv
print('The script is called:', script)
print('Your first variable is:', first)
print('Your second variable is:', second)
print('Your third variable is:', third)
应该看到的结果
The script is called: C:/Users/PycharmProjects/python3_project/ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd
附加练习
1,给脚本3个以下的参数,会得到错误信息。
Traceback (most recent call last):
File "C:/Users/PycharmProjects/python3_project/ex13.py", line 3, in
script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 3)
因为我们设置的参数变量包括脚本本身是4个:script, first, second, third, 如果除了脚本少于3个,程序会出错。
2,
接受更少参数的脚本
from sys import argv
script, first, second = argv
print('The script is called:', script)
print('Your first variable is:', first)
print('Your second variable is:', second)
结果是
The script is called: C:/Users/PycharmProjects/python3_project/ex13-1.py
Your first variable is: apple
Your second variable is: orange
接受更多参数的脚本
from sys import argv
script, first, second, third, fourth, fifth = argv
print('The script is called:', script)
print('Your first variable is:', first)
print('Your second variable is:', second)
print('Your third variable is:', third)
print('Your third variable is:', fourth)
print('Your third variable is:', fifth)
结果是
The script is called: C:/Users/PycharmProjects/python3_project/ex13-2.py
Your first variable is: Learn
Your second variable is: Python
Your third variable is: the
Your third variable is: Hard
Your third variable is: Way
3,将input和argv一起使用
from sys import argv
script, first, second = argv
first = input("> ")
second = input("> ")
print('The script is called:', script)
print('Your first variable is: %s' % first)
print('Your second variable is: %s' % second)
> Hello
> Baby
The script is called: C:/Users/PycharmProjects/python3_project/ex13-3.py
Your first variable is: Hello
Your second variable is: Baby
4, 模块需要导入(import),比如from sys import argv,把 sys模块导入进来,模块也称为库(library)。
习题13 心得体会:
一,在命令行中运行带参数的脚本时,可以直接把脚本名及其参数列上,如 python ex13.py first 2nd 3rd, 但是在Pycharm中运行脚本,需要设置脚本的参数,在Run--Edit configurations... 中设置Script parameters.
二,参数是变量,与input函数一起使用时,在print模块需要引用变量, %s % first
习题14 提示和传递
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 likeing me. '
'\nYou live in %r. Not sure where that is. '
'\nAnd you have a %r computer. Nice.' %(likes, lives, computer))
结果
Hi Faye, I'm the C:/Users/PycharmProjects/python3_project/ex14.py script.
I'd like to ask you a few questions.
Do you like me Faye
> Yes
Where do you live Faye?
> Beijing
What kind of computer do you have?
> Lenovo
Alright, so you said 'Yes' about likeing me.
You live in 'Beijing'. Not sure where that is.
And you have a 'Lenovo' computer. Nice.
附加练习
2,3题
from sys import argv
script, user_name, one_person = 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('What a nice day, %s' % one_person)
one_person = 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 likeing me. '
'\nYou live in %r. Not sure where that is. '
'\nAnd you have a %r computer. Nice.'
'\nWhat a nice day, %s'%(likes, lives, computer, one_person))
习题15 读取文件
from sys import argv #从模块sys导入参数argv
script, filename = argv # 参数包含script和filename
txt = open(filename) #txt方法是打开文件
print('Here\'s your file %r:' % filename) #打印“Here's your file ‘ex15_sample.txt’”
print(txt.read()) #print txt的文件内容
print('Type the filename again:') #打印'Type the filename again:'
file_again = input("> ") # 输入文件名
txt_again = open(file_again) # 打开文件名
print(txt_again.read()) # 打印所输入文件名的文件内容
应该看到的结果
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
> ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
附加练习
5,用自定义输入的方法获取文件名称更好,这样,这段代码可以重复使用,打开很多文件。
6
习题16 读写文件
from sys import argv #从sys模块引入参数变量argv
script, filename = argv # 参数解包成script 和 filename
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() #清空目标文件
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() # 关闭目标文件
应该看到的结果
We're going to erase 'test.txt'.
If you don't want that, hit CTRL-C(^C.)
If you do want that, hit RETURN.
?
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines.
line 1: Mary had a little lamb
line 2: It's fleece was white as snow
line 3: It was also tasty
I'm going to write these to the file.
And finally, we close it.
附加练习
3,
from sys import argv #从sys模块引入参数变量argv
script, filename = argv # 参数解包成script 和 filename
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() #清空目标文件
print('I\'m going to write these to the file.') # 打印
target.write('Mary had a little lamb \nIt\'s fleece was white as snow \nIt was also tasty')
print('And finally, we close it.')
target.close() # 关闭目标文件
4,给open赋予一个‘w’参数,打开文件并且是写入模式。
习题17 更多文件操作
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 coud do these two on one line too, how?
in_file = open(from_file)
indata = in_file.read()
print('The input file is %d bytes long' % len(indata))
print('Does the output file exist? %r' % exists(to_file))
print('Ready, hit RETURN to continue, CTRL-C to abort.')
input()
out_file = open(to_file, 'w')
out_file.write(indata)
print('Alright, all done')
out_file.close()
in_file.close()
应该看到的结果
Copying from cat.txt to test.txt
The input file is 25 bytes long
Does the output file exist? True
Ready, hit RETURN to continue, CTRL-C to abort.
yes
Alright, all done
附加练习
6,为什么需要在代码中写output.close()? 因为open函数的返回值是一个文件句柄, 这个句柄从操作系统托付给你的Python程序。一旦处理完文件,要归还这个文件句柄,只有这样程序不会超出一次能打开的文件句柄的数量上限。
心得:习题17的附加练习的题目还不理解,需要继续学习。