#!/usr/bin/python # -*- coding: utf-8 -*- # 9–1. 文件过滤. 显示一个文件的所有行, 忽略以井号( # )开头的行. # 这个字符被用做Python, Perl, Tcl, 等大多脚本文件的注释符号. # 附加题: 处理不是第一个字符开头的注释. import os, sys filename = "" for filename in os.listdir(os.getcwd()): if os.path.splitext(filename)[1] == ".py": break fobj = open(filename, "r") print print '*' * 25 for 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 os number = int(raw_input("Please input the how lines to show: ")) filename = raw_input("Please input the file name: ") print filename if 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 os filename = 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 os filename = raw_input("Please input file name: ") fobj = open(filename, "r") show_number = 0 for 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+1 fobj1.close() fobj2.close()
#!/usr/bin/python # -*- coding: utf-8 -*- # 9–13. 命令行参数 # a) 什么是命令行参数, 它们有什么用? # b) 写一个程序, 打印出所有的命令行参数. import sys for arg in sys.argv: print arg
#!/usr/bin/python # -*- coding: utf-8 -*- # 9–15. 复制文件. 提示输入两个文件名(或者使用命令行参数). # 把第一个文件的内容复制到第二个文件中去. import sys def 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 = 0 for line in fobj: count += line.count(ch) else: print count