本帖最后由 ligxi 于 2020-4-7 15:18 编辑
大概写了一下,应该可以满足你的基本要求了。
打包好的exe程序,由于是用Python打包的,所以生成的exe程序比原生大点,不过这都不是问题:
https://www.lanzouj.com/ib1qhte
Python源码:
[Python] 纯文本查看 复制代码 import os
# 工作目录
root = os.getcwd()
# 处理函数
deWeight_and_sort = lambda x: ' '.join(sorted(list(set(x.strip().split(' ')))))
# 主逻辑
while True:
form = input('请输入待去重排序的完整文件名或数字,输入N/n则退出本程序:')
if not form:
print('输入的内容为空,请重新输入!')
continue
elif form == 'n' or form == 'N':
print('本程序结束!')
break
else:
path = os.path.join(root, form)
if os.path.isfile(path): # 判断是否是文件
print('输入的是个文件!本次去重排序结果为:')
with open(path, 'r', encoding='gbk') as f1:
f2 = open('New-%s' % form, 'w')
for item in f1:
if item == '\n': # 如果为空,则跳过
continue
item_lst = deWeight_and_sort(item)
print(item_lst)
f2.write(item_lst + '\n')
f2.flush()
f2.close()
print('本次文件去重排序的结果将写入新文件中,新文件名为:New-%s' % form)
else:
print('输入的不是文件!本次去重排序结果为:')
item_lst = deWeight_and_sort(form)
print(item_lst)
print()
os.system("pause")
测试的结果:
|