在python中以特定顺序读取文件 [英] Reading files in a particular order in python
问题描述
让我们说我在一个文件夹中有三个文件:file9.txt,file10.txt和file11.txt,我想按此特定顺序阅读它们.有人可以帮我吗?
Lets say I have three files in a folder: file9.txt, file10.txt and file11.txt and i want to read them in this particular order. Can anyone help me with this?
现在我正在使用代码
import glob, os
for infile in glob.glob(os.path.join( '*.txt')):
print "Current File Being Processed is: " + infile
它首先读取file10.txt,然后读取file11.txt,然后读取file9.txt.
and it reads first file10.txt then file11.txt and then file9.txt.
有人可以帮助我如何获得正确的订单吗?
Can someone help me how to get the right order?
推荐答案
文件系统上的文件未排序.您可以使用 sorted()
函数:
Files on the filesystem are not sorted. You can sort the resulting filenames yourself using the sorted()
function:
for infile in sorted(glob.glob('*.txt')):
print "Current File Being Processed is: " + infile
请注意,代码中的os.path.join
调用是无操作的;仅带有一个参数,它什么也不会做,只会使该参数保持不变.
Note that the os.path.join
call in your code is a no-op; with only one argument it doesn't do anything but return that argument unaltered.
请注意,您的文件将按字母顺序排序,这会将10
放在9
之前.您可以使用自定义键功能来改善排序:
Note that your files will sort in alphabetical ordering, which puts 10
before 9
. You can use a custom key function to improve the sorting:
import re
numbers = re.compile(r'(\d+)')
def numericalSort(value):
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
for infile in sorted(glob.glob('*.txt'), key=numericalSort):
print "Current File Being Processed is: " + infile
numericalSort
函数将文件名中的所有数字分开,将其转换为实际数字,然后返回结果进行排序:
The numericalSort
function splits out any digits in a filename, turns it into an actual number, and returns the result for sorting:
>>> files = ['file9.txt', 'file10.txt', 'file11.txt', '32foo9.txt', '32foo10.txt']
>>> sorted(files)
['32foo10.txt', '32foo9.txt', 'file10.txt', 'file11.txt', 'file9.txt']
>>> sorted(files, key=numericalSort)
['32foo9.txt', '32foo10.txt', 'file9.txt', 'file10.txt', 'file11.txt']
这篇关于在python中以特定顺序读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!