【Python爬虫】-第二周 习题 13-17 (解包,参数,文件读写等)

习题 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")进行处理。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,858评论 0 17
  • 习题13 参数、解包、变量代码: 加分习题:13.3 将raw_input和argv一起使用,让你的脚本从用户的手...
    奔跑的Kay阅读 3,698评论 2 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,949评论 19 139
  • 我在去月亮的路途 写了一封信给你 一边写一边诉苦 可以让我这样就落幕 什么时候可以写完 什么时候可以结束 你以轻快...
    极乐一切阅读 1,531评论 5 3
  • 看来你真的承受不住我的好
    会撒娇的小白鸽阅读 775评论 0 0

友情链接更多精彩内容