【Python爬虫】-第二周作业(笨办法学Python13-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)
···

【Python爬虫】-第二周作业(笨办法学Python13-17)_第1张图片
Paste_Image.png
# 习题14:提示和传递
from sys import argv
script, user_name = argv
prompt = '> '
print("Hi %r, 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.
""" % (likes, lives, computer))
【Python爬虫】-第二周作业(笨办法学Python13-17)_第2张图片
Paste_Image.png

···
变量要对应,一个不能多,一个不能少
···

···

习题15:提取文件

from sys import argv

script, filename = argv
txt = open(filename)
print('Here's your file %r:' % filename)
print(txt.read())
print('Type the filename again:')
file_again = input("> ")
txt_again = open(file_again)
print(txt_again.read())

···

【Python爬虫】-第二周作业(笨办法学Python13-17)_第3张图片
Paste_Image.png

···

习题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() #清空目标文件

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() # 关闭文件

···

【Python爬虫】-第二周作业(笨办法学Python13-17)_第4张图片
Paste_Image.png

···

习题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))

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()

···

【Python爬虫】-第二周作业(笨办法学Python13-17)_第5张图片
Paste_Image.png

得授权,非商业转载请注明出处。

你可能感兴趣的:(【Python爬虫】-第二周作业(笨办法学Python13-17))