小甲鱼python29课n-m行文本读取

def recep(reception):
'''
reception:接收
scope:范围
:param reception:
:return:
'''
if reception.startswith(":"):
# 取scope[1]
scope_start = reception.split(':')
return 'scope_start',int(scope_start[1])
elif reception.endswith(":"):
# 取scope[0]
scope_end = reception.split(':')
return 'scope_end',int(scope_end[0])
else:
# 取scope[begin:end]
scope_s_e = reception.split(':')
return 'scope_s_e',[int(scope_s_e[0]),int(scope_s_e[1])]
def file_defi(file,scope=[]):
with open(file,'r') as f:
list1=f.readlines()
length=len(list1)
if scope[0]=='scope_start' and scope[1]>=0 and scope[1]<=length:
for i in range(0,scope[1]):
print(list1[i].strip())
elif scope[0] == 'scope_end' and scope[1]>=1 and scope[1]<=length:
for i in range(scope[1]-1,length):
print(list1[i].strip())
elif scope[0]=='scope_s_e' and scope[1][0]>=0 and scope[1][1]<=length:
for i in range(scope[1][0]-1,scope[1][1]):
print(list1[i].strip())
else:
print('输入范围有误!')

'''
接收用户的输入
'''

file=input('输入要查找的文件:').strip()
reception=(str(input('请输入读取的范围(:n,n:,n:m):'))).strip()
temp=recep(reception)
file_defi(file,temp)

你可能感兴趣的:(小甲鱼python29课n-m行文本读取)