《笨办法学python》加分习题20——我的答案
来源:互联网 发布:淘宝送的鱼骨头怎么用 编辑:程序博客网 时间:2024/05/22 17:12
这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!
文中例题:
from sys import argvscript, input_file = argvdef print_all(f): print f.read()def rewind(f): f.seek(0)def print_a_line(line_count, f): print line_count, f.readline()current_file = open(input_file)print "First let's print the whole file:\n"print_all(current_file)print "Now let's rewind, king of like a tape."rewind(current_file)print "Let's print three lines:"current_line = 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)
运行结果:
习题答案:
1、
# -- coding: utf-8 --# 从系统中引入argvfrom sys import argv# 引入一个参量,script为脚本名称script, input_file = argv# 打印脚本fdef print_all(f): print f.read()# 设置开始的偏移量,即从偏移位置开始运行def rewind(f): f.seek(1)# 打印def print_a_line(line_count, f): print line_count, f.readline()# 打开文件,将文件内容复制到current_file中current_file = open(input_file)print "First let's print the whole file:\n"print_all(current_file)print "Now let's rewind, king of like a tape."rewind(current_file)print "Let's print three lines:"current_line = 1# 传入参数current_line = 1 和文件 current_fileprint_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)
2、就是一步一步带进去的。
4、如一中注释
转载至: http://www.runoob.com/python/file-seek.html
感谢菜鸟教程!
5、
count = 1temp = 2count += tempprint count
阅读全文
0 0
- 《笨办法学python》加分习题20——我的答案
- 《笨办法学python》加分习题1——我的答案
- 《笨办法学python》加分习题2——我的答案
- 《笨办法学python》加分习题3——我的答案
- 《笨办法学python》加分习题4——我的答案
- 《笨办法学python》加分习题5——我的答案
- 《笨办法学python》加分习题6——我的答案
- 《笨办法学python》加分习题7——我的答案
- 《笨办法学python》加分习题8——我的答案
- 《笨办法学python》加分习题9——我的答案
- 《笨办法学python》加分习题10——我的答案
- 《笨办法学python》加分习题11——我的答案
- 《笨办法学python》加分习题12——我的答案
- 《笨办法学python》加分习题13——我的答案
- 《笨办法学python》加分习题14——我的答案
- 《笨办法学python》加分习题15——我的答案
- 《笨办法学python》加分习题16——我的答案
- 《笨办法学python》加分习题17——我的答案
- Spring boot统一异常处理
- 算法竞赛入门经典java版程序ch3 Uva10340
- PrjImageLazyLoad
- Codeforces Round #341 (Div. 2) D. Rat Kwesh and Cheese (pow,复数
- 算法复杂度
- 《笨办法学python》加分习题20——我的答案
- NYOJ-518-取球游戏
- 51nod 1682-中位数计数(前缀和)
- CodeForces
- 流媒体平台分析
- 【深度学习入门—2015MLDS】1. What is Machine Learning, Deep Learning and Structured Learning?
- .NET快速信息化系统开发框架 V3.2->Web版本新增“文件管理中心”集上传、下载、文件共享等一身,非常实用的功能
- [转]做个男人,做个成熟的男人,做个有城府的男人
- tmux分屏解决方案