Python 文件排序工具:按字母顺序排序文件内容
您可以使用以下 Python 代码来实现这个功能:
def sort_file_content():
input_file = input('请输入文件名(包括路径):')
output_file = 'sorted_data.txt'
lines_written = 0
try:
with open(input_file, 'r') as file:
content = file.readlines()
sorted_content = sorted(content)
with open(output_file, 'w') as file:
for line in sorted_content:
file.write(line)
lines_written += 1
print('成功写入的行数:', lines_written)
except FileNotFoundError:
print('文件未找到!')
sort_file_content()
请确保文件路径是正确的,并且您具有对输入和输出文件的读写权限。
原文地址: https://www.cveoy.top/t/topic/lLt 著作权归作者所有。请勿转载和采集!