""" 9-1 """ f=open('test.txt','r') for i in f: if i[0:1]=='#': continue print(i) f.close() """ 9-1-1 """ import re def isNote(str): m=re.match(r'^[ ]*#',str) if m is None: return False else: return True f=open('test.txt','r') for i in f: if isNote(i): continue print(i) f.close() """ 9-2 """ n=int(input('enter a number: ')) filename=input('enter a file name: ') j=1 f=open(filename,'r') for i in f: if j>n: break print(i) j+=1 f.close() """ 9-3 """ filename=input('enter a file name: ') f=open(filename,'r') l=[i for i in f] print(len(l)) # print(len([i for i in f])) j=0 for i in f: j+=1 print(j) f.close() """ 9- 4 """ filename=input('enter a file name: ') f=open(filename,'r',encoding='utf-8') j=1 for i in f: # print(j,j%26) print( i) if j % 25 == 0: c = input('press anykey to continue...') if c !='': continue j+=1 f.close() """ 9-5 """ """ 9-6 """ f1=open('test.txt','r') f2=open('test1.txt','r') flag=True r=1 while True: try: line1=next(f1) line2 = next(f2) except(StopIteration): print(r,1) break for i in range(0,min(len(line1),len(line2))): # print(line1[i],' ', line2[i]) if line1[i]!=line2[i]: print(r,i+1) flag=False break if not flag: break r+=1 """ 9-7 """ import os f=open('C:\Windows\win.ini','r') basedir='D:\PycharmProjects\pythonProject\ccdata' dir='practice9' os.chdir(basedir) if not os.path.isdir(dir): os.mkdir(dir) os.chdir(dir) # print(os.getcwd()) for i in f: filename='' item=[] if i[0:1]=='[': i=i[1:len(i)-2] filename=i.split(' ')[0] try: if file: file.close() print(file.closed) except(NameError): pass file = open(filename, 'w') # print(filename) elif i[0:1] !=';': item=i.split('=') file.write('%s %s' % (item[0], item[1])) # print(item) """ 9-12-1 """ import time import operator import base64 import re import os import pickle def now_to_time(t): return time.strftime("%Y/%m/%d %H:%M:%S",time.localtime(t)) def str_to_time(t): timeArray= time.strptime(t,"%Y/%m/%d %H:%M:%S") return int(time.mktime(timeArray)) def loginuser(): p='Enter login name,case_insensitive: ' while True: name=input(p).strip().lower() icon=re.search(r'\W',name) print(name,len(name)) if len(name)==0: p='Name is space,try again : ' continue elif icon!=None: p = 'Name contains icon,try again : ' continue else: break if name not i
Python核心编程第二版 第九章课后练习题
最新推荐文章于 2023-04-18 00:00:00 发布
本文详细解答了Python编程第二版第九章的多个练习题,包括文件操作、循环控制、用户交互、文件比较、目录管理和压缩解压等实战内容,适合巩固Python基础操作。
摘要由CSDN通过智能技术生成