马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 风不会停息 于 2018-7-20 15:56 编辑
5个动动手程序:
def file_write(file_name):
f = open('python_study\\' + str(file_name) + '.txt', 'x')
print("请输入内容【单独输入':w'保存退出】: ")
while True:
file_content = input( )
if file_content != ':w':
f.write('%s\n' % file_content)
else:
break
f.close()
file_name = input("请输入文件名: ")
file_write(file_name)
def file_compare(file1, file2):
f1 = open('python_study\\' + str(file1) + '.txt')
f2 = open('python_study\\' + str(file2) + '.txt')
count_line = 0
count_char = 0
differ_line = []
differ_char = []
for each_line1 in f1:
each_line2 = f2.readline()
count_line += 1
if each_line1 != each_line2:
differ_line.append(count_line)
for each_char in each_line1:
count_char += 1
if each_char != each_line2[count_char - 1]:
differ_char.append(count_char)
break
count_char = 0
f1.close()
f2.close()
differ = [differ_line, differ_char]
return differ
file1 = input('请输入需要比较的第一个文件名: ')
file2 = input('请输入需要比较的第二个文件名: ')
differ = file_compare(file1, file2)
length = len(differ[0])
if length == 0:
print('两个文件一样')
else:
print('这两个文件共有 %d 处不同' % length)
for each in range(length):
print( '第 %d 行, 第 %d 个字符开始不一样' % (differ[0][each], differ[1][each]) )
def file_print(file, n):
f = open('python_study\\' + str(file) + '.txt')
count = 0
for each_line in f:
if count == n:
break
print(each_line)
count += 1
f.close()
file = input("请输入要打开的文件: ")
n = input("请输入需要显示该文件前几行: ")
while not n.isdigit():
n = input("输入格式错误, 请重新输入: ")
n = int(n)
file_print(file, n)
def file_print(file, range_line):
f = open('D:\\python\\python_study\\' + str(file) + '.txt')
count = 0
range_line = range_line.partition(':')
length = len(range_line)
if range_line[0] != '' and range_line[2] != '':
print('文件 %s 从第 %d 行到第 %d 行的内容如下: ' % ( file + '.txt', int(range_line[0]), int(range_line[2]) ) )
for each_line in range( int(range_line[0]) - 1 ):
f.readline()
for each_line in f:
if count <= int( range_line[2] ) - int( range_line[0] ):
print(each_line)
count += 1
else:
break
elif range_line[0] == '' and range_line[2] != '':
print('文件 %s 从第开始到第 %d 行的内容如下: ' % ( file + '.txt', int(range_line[2]) ) )
for each_line in f:
if count == int (range_line[2] ):
break
else:
print(each_line)
count += 1
elif range_line[2] == '' and range_line[0] != '':
print('文件 %s 从第 %d 行到末尾的内容如下: ' % ( file + '.txt', int(range_line[0]) ) )
for each_line in range( int(range_line[0]) - 1 ):
f.readline()
for each_line in f:
print(each_line)
elif length == 3 and range_line[0] == '' and range_line[2] == '':
print('文件 %s 全文的内容如下: ' % ( file + '.txt' ) )
for each_line in f:
count += 1
print(each_line)
f.close()
file = input("请输入要打开的文件: ")
range_line = input("请输入需要显示的格式【格式如 13:21 或 :21 或 21:】: ")
file_print(file, range_line)
def file_replace(file, old, new):
f = open('D:\\python\\python_study\\' + str(file) + '.txt')
count = 0
content = []
for each_line in f:
count += each_line.count(old)
each_line = each_line.replace(old, new)
content.append(each_line)
print("文件 %s 中共有 %d 个 【%s】" % (file + '.txt', count, old) )
print("您确定要把所有的 【%s】 替换为 【%s】 吗?" % ( old, new ) )
judge = input('【YES/NO】: ')
if judge == 'YES':
f_write = open('D:\\python\\python_study\\' + str(file) + '.txt', 'w')
f_write.writelines(content)
f_write.close()
f.close()
file = input("请输入文件名: ")
old = input("请输入需要替换的单词或字符: ")
new = input("请输入新的单词或字符: ")
file_replace(file, old, new)
|