import os.path
import linecache
path = r"F:\ljwdata\data\Gabiondata\anno_txt"
path_list = os.listdir(path)
path_list.sort(key=lambda x:int(x.split('.txt')[0]))
files = []
for filename in path_list:
file_names = os.path.join(filename)
# print(file_names)
str2list = file_names.split()
# print(str2list)
files.extend(str2list)
# print(files)
# print(type(files))
file_ob_list = []
for file in files:
fileob = path + '\\' +file
file_ob_list.append(fileob)
print(file_ob_list)
print(type(file_ob_list))
ldata = []
data = []
line_num = 1
total_line = len(open(file_ob_list[0]).readlines())
# print(total_line)
while line_num <= total_line:
for file_ob in file_ob_list:
line = linecache.getline(file_ob, line_num)
line = line.strip()
if line is None or len(line)==0 :
break
fields = line.split('\t')
data.append(fields)
# prob = fields[1]
# if fields[1] != 'NA':
# prob = float(fields[1])
# prob = '%.4f'%prob
#
# if file_ob == file_ob_list[0]:
# data = [fields[0],prob]
#
# else:
# data.append(prob)
line_num = line_num + 1
ldata.append(data)
data = []
f = open("F:/ljwdata/data/Gabiondata/gabion.txt", "w+")
# i = 0
# j = 0
for i, p in enumerate(ldata):
print(p)
while i < len(p):
p[i] = p[i][0].split(',')
i = i + 1
# p[i] = p[i][i].split(',') # a, b, c, d
# a = float(a) # np.float32
# for q in range(len(p)): # [['', '', '','']]
for j, q in enumerate(p):
# q[j] = q[j].split(',')
# print(q[0])
# q[0] = float(q[0])
# f.write(q[0] + "\n")
# q[1] = float(q[1])
# f.write(q[0] + ",")
# q[2] = float(q[2])
# f.write(q[0] + ",")
# q[3] = float(q[3])
# q[0] = map(lambda x : float(x), q[0])
# print(type(q))
f.write(q[0] + " ")
f.write(q[1] + " ")
f.write(q[2] + " ")
f.write(q[3] + "\n")
# i = i + 1
# j = j + 1
# print(i)
f.write("\n")
f.close()
在很多时候,我们都需要使用Python对某个文件夹下的文件进行文件名排序读取,就是一种按照文件名中数字的值大小进行的排序,这是Windows系统下的文件夹自然的排序方式。
然而,当我使用Python对其直接进行读取时,发现其结果排序方式为1,10,100,1000,其原因为:Python在读取时实际上是采用字符串的ASCII值大小进行的排序。