python核心编程--第九章 9.11 练习
来源:互联网 发布:英语翻译 知乎 编辑:程序博客网 时间:2024/05/17 08:43
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–1. 文件过滤. 显示一个文件的所有行, 忽略以井号( # )开头的行.# 这个字符被用做Python, Perl, Tcl, 等大多脚本文件的注释符号.# 附加题: 处理不是第一个字符开头的注释.import os, sysfilename = ""for filename in os.listdir(os.getcwd()): if os.path.splitext(filename)[1] == ".py": breakfobj = open(filename, "r")printprint '*' * 25for line in fobj: if line[0] != '#': print line,<pre name="code" class="python"><pre name="code" class="python">fobj.close()
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–2. 文件访问. 提示输入数字 N 和文件 F, 然后显示文件 F 的前 N 行.import osnumber = int(raw_input("Please input the how lines to show: "))filename = raw_input("Please input the file name: ")print filenameif not os.path.exists(filename): print "File not exist."else: fobj = open(filename, "r") show_number = 0 for line in fobj: if show_number > number: break print line, show_number += 1<pre name="code" class="python"><pre name="code" class="python">fobj.close()
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–3. 文件信息. 提示输入一个文件名, 然后显示这个文本文件的总行数.import osfilename = raw_input("Please input file name: ")fobj = open(filename, "r")print len(lines = fobj.readlines())<pre name="code" class="python"><pre name="code" class="python">fobj.close()
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–4. 文件访问. 写一个逐页显示文本文件的程序. 提示输入一个文件名, 每次显示# 文本文件的 25 行, 暂停并向用户提示"按任意键继续.", 按键后继续执行.import osfilename = raw_input("Please input file name: ")fobj = open(filename, "r")show_number = 0for f in fobj: print f, show_number += 1 if show_number % 25 == 0: # print "Any key to continue..." os.system('pause')<pre name="code" class="python">fobj.close()
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–6. 文件比较. 写一个比较两个文本文件的程序. 如果不同, 给出第一个不同处的行号和列号.fobj1 = open("9-6.py", "r")fobj2 = open("9-3.py", "r")length = max(len(fobj1.readlines()), len(fobj2.readlines()))fobj1.seek(0)fobj2.seek(0)for linenum in range(0, length): line1 = fobj1.readline() line2 = fobj2.readline() if line1 != line2: print "diff at", linenum+1 break else: if len(fobj1.readlines()) == len(fobj2.readlines()): print "same" else: print length+1fobj1.close()fobj2.close()
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–13. 命令行参数# a) 什么是命令行参数, 它们有什么用?# b) 写一个程序, 打印出所有的命令行参数.import sysfor arg in sys.argv: print arg
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–15. 复制文件. 提示输入两个文件名(或者使用命令行参数).# 把第一个文件的内容复制到第二个文件中去.import sysdef printhelp(): print "Useage: ", sys.argv[0], "SOURCE TARGET" if len(sys.argv) != 3: printhelp()else: sourcename = sys.argv[1] targetname = sys.argv[2] fsource = open(sourcename, "r") ftarget = open(targetname, "w") for line in fsource: ftarget.write(line) fsource.close() ftarget.close()
#!/usr/bin/python# -*- coding: utf-8 -*-# 9–16. 文本处理. 人们输入的文字常常超过屏幕的最大宽度. 编写一个程序,# 在一个文本文件中查找长度大于 80 个字符的文本行. 从最接近 80 个# 字符的单词断行, 把剩余文件插入到下一行处.# 程序执行完毕后, 应该没有超过 80 个字符的文本行了.fsource = open("x", "r")ftarget = open("no-80.txt", "w")for line in fsource: if len(line)-1 <= 80: ftarget.write(line) else: writeline = line while len(writeline)-1 > 80: i = 79 while i >= 0: if writeline[i] in [' ', ',', ':', ';', '.', '!']: ftarget.write(writeline[0:i+1] + '\n') writeline = writeline[i+1:] break; i -= 1 ftarget.write(writeline) fsource.close()ftarget.close()
#!/usr/bin/python# -*- coding: utf-8 -*-# 9.18. 搜索文件. 提示输入一个字节值(0 - 255)和一个文件名.# 显示该字符在文件中出现的次数.filename = raw_input("Please input file name: ")ch = raw_input("Please input the char for find: ")fobj = open(filename)count = 0for line in fobj: count += line.count(ch)else: print count
0 0
- python核心编程--第九章 9.11 练习
- Python核心编程 第九章练习
- python核心编程第九章练习
- Python核心编程 第九章
- 《Python核心编程》第九章
- python 核心编程 第九章
- Python核心编程第九章笔记
- python核心编程第九章答案
- python核心编程第九章(9-11)
- python核心编程第九章(9-10)
- python核心编程第九章(9-12)
- python 核心编程第九章练习题
- Python核心编程-练习
- 《Python核心编程》第九章编程题代码
- Python核心编程 练习 第二章
- Python核心编程 练习 第五章
- Python核心编程 练习 第六章
- 《python核心编程》第二章练习答案
- 黑马程序员——集合框架
- php读取csv文件类
- c笔记——位运算
- Linux命令之fuser详解
- 收缩表段(shrink space)
- python核心编程--第九章 9.11 练习
- 经纬财富:洛阳现货白银交易时间现货白银交易手续费
- [JAVA入门之错误总结-2]项目创建-各种项目类型之间的区别
- leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)
- 机制和策略
- mongodb 启动脚本
- 【Swift】延迟存储属性
- DSP中GEL文件的作用
- 我是菜鸟级的项目经理,但要努力成为背黑锅的人,fighting!