马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
动动手:
0.
lines = []
file_name = input('请输入文件名:')
print("请输入内容(单独输入':w'保存退出):")
words = input() + '\n'
while words != ':w\n':
line = lines.append(words)
words = input() + '\n'
f = open('/Users/yufan/Documents/python编程/29/29dds0/%s' % file_name,'x',encoding = 'GBK')
f.writelines(lines)
f.close()
1.
f1 = open('/Users/yufan/Documents/python编程/29/29dds1/余二娃',encoding = 'GBK')
f2 = open('/Users/yufan/Documents/python编程/29/29dds1/余三娃',encoding = 'GBK')
f1_string = []
f2_string = []
count = 0
wrong_line = []
for file_line in f1:
f1_string.append(file_line)
for file_line in f2:
f2_string.append(file_line)
small_len = len(f1_string) if len(f1_string) < len(f2_string) else len(f1_string)
for each_index in range(small_len):
if f1_string[each_index] != f2_string[each_index]:
count += 1
wrong_line.append(count)
else:
count += 1
print('两个文件一共有%d处不同:' % len(wrong_line))
for each in wrong_line:
print('第 %d 行不一样' % each)
f2.close()
f1.close()
2.
file_name = input('请输入你要打开的文件(/Users/yufan/Documents/python编程/29/29dds2)下:')
row = int(input('请输入需要显示该文件的前几行:'))
f = open('/Users/yufan/Documents/python编程/29/29dds2/%s' % file_name, encoding = 'GBK')
L = list(f)
n = 0
while n < row:
print(L[n])
n += 1
f.close()
3.
file_name = input('请输入你要打开的文件(/Users/yufan/Documents/python编程/29/29dds2)下:')
f = open('/Users/yufan/Documents/python编程/29/29dds2/%s' % file_name, encoding = 'GBK')
L = list(f)
string = []
(start,end) = input('请输入需要显示该文件的行数(例如 13:21 或 :21 或 21:):').split(':',1)
if start == '':
start = 1
else:
start = int(start)
if end == '':
end = len(L)
else:
end = int(end)
while True:
if start > len(L) or start < 0 or end >len(L):
print('你输入的数字超出最大范围,请重新输入!')
(start,end) = input('请输入需要显示该文件的行数(例如 13:21 或 :21 或 21:):').split(':',1)
if start == '':
start = 1
else:
start = int(start)
if end == '':
end = len(L)
else:
end = int(end)
else:
break
n = start
while n <= end:
string.append((L[n - 1]))
n += 1
if start != 0 and end != 0:
print('文件%s从第%d行到第%d行的内容如下:' % (file_name,start,end))
elif start == 1 and end != 0:
print('文件%s从第开始到第%d行的内容如下:' % (file_name,end))
elif end ==len(string) and start != 1:
print('文件%s从第%d行到末尾的内容如下:' % (file_name,start))
else:
print('你在逗我,0到0行噢?')
for each_line in string:
print(each_line)
f.close()
|