一,作業(yè)內容
笨辦法學Python習題13-17
二,作業(yè)代碼
習題13 參數(shù)灶挟、解包和變量
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個以下的參數(shù)毒租,會得到錯誤信息稚铣。
Traceback (most recent call last):
File "C:/Users/PycharmProjects/python3_project/ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 3)
因為我們設置的參數(shù)變量包括腳本本身是4個:script, first墅垮, second, third, 如果除了腳本少于3個惕医,程序會出錯。
2算色,
接受更少參數(shù)的腳本
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
接受更多參數(shù)的腳本
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 心得體會:
一,在命令行中運行帶參數(shù)的腳本時能岩,可以直接把腳本名及其參數(shù)列上寞宫,如 python ex13.py first 2nd 3rd, 但是在Pycharm中運行腳本,需要設置腳本的參數(shù)拉鹃,在Run--Edit configurations... 中設置Script parameters.
二辈赋,參數(shù)是變量,與input函數(shù)一起使用時膏燕,在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導入?yún)?shù)argv
script, filename = argv # 參數(shù)包含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模塊引入?yún)?shù)變量argv
script, filename = argv # 參數(shù)解包成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模塊引入?yún)?shù)變量argv
script, filename = argv # 參數(shù)解包成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’參數(shù)击纬,打開文件并且是寫入模式。
習題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函數(shù)的返回值是一個文件句柄更振, 這個句柄從操作系統(tǒng)托付給你的Python程序。一旦處理完文件饭尝,要歸還這個文件句柄肯腕,只有這樣程序不會超出一次能打開的文件句柄的數(shù)量上限。
心得:習題17的附加練習的題目還不理解钥平,需要繼續(xù)學習实撒。