txtfilepath = 'D:\\桌面\\dnf1\\dnf-00002.txt'
list = []
with open(txtfilepath, 'r') as f:
for line in f:
list.append(line.strip())
with open(txtfilepath, "w" ) as f:
for item in sorted(list):
f.writelines(item)
f.writelines('\n')
f.close()
文件夹下txt文件内容排序
import os
import random
import numpy as np
from numpy import *
# txtfilepath = 'D:\\桌面\\dnf1\\' #原始txt文件所存文件夹,文件夹可以有一个或多个txt文件
# savefilepath = 'D:\\桌面\\dnf1\\' #更改后txt文件存放的文件夹
txtfilepath = r'D:\视频\dnf_jian_txt'
savefilepath = r'D:\视频\dnf_jian_txt'
total_txt = os.listdir(txtfilepath) # 返回指定的文件夹包含的文件或文件夹的名字的列表
num = len(total_txt)
list = range(num) #创建从0到num的整数列表
files = os.listdir(savefilepath)
for i in list: #遍历每一个文件
name = total_txt[i]
list = []
with open(txtfilepath+"/"+name, 'r') as f:
for line in f:
list.append(line.strip())
with open(txtfilepath+"/"+name, 'w') as f:
for item in sorted(list):
f.writelines(item)
f.writelines('\n')
f.close()