python大文件排序_如何用Python对大文件进行排序?

我在activestate.com上找到了一些很有前途的代码来对大文件进行排序。我试图在Ubuntu 10.04上的默认Python 2.6.5解释器上运行它。当我尝试在一个小的测试文件上运行它时,我得到了下面的错误跟踪。我在activestate.com上寻求帮助,但是这个线程已经沉默了18个月。这里有人看到明显的解决方案吗?

谢谢。## {{{ http://code.activestate.com/recipes/576755/ (r3)

# based on Recipe 466302: Sorting big files the Python 2.4 way

# by Nicolas Lehuen

import os

from tempfile import gettempdir

from itertools import islice, cycle

from collections import namedtuple

import heapq

Keyed = namedtuple("Keyed", ["key", "obj"])

def merge(key=None, *iterables):

# based on code posted by Scott David Daniels in c.l.p.

# http://groups.google.com/group/comp.lang.python/msg/484f01f1ea3c832d

if key is None:

keyed_iterables = iterables

else:

keyed_iterables = [(Keyed(key(obj), obj) for obj in iterable)

for iterable in iterables]

for element in heapq.merge(*keyed_iterables):

yield element.obj

def batch_sort(input, output, key=None, buffer_size=32000, tempdirs=None):

if tempdirs is None:

tempdirs = []

if not tempdirs:

tempdirs.append(gettempdir())

chunks = []

try:

with open(input,'rb',64*1024) as input_file:

input_iterator = iter(input_file)

for tempdir in cycle(tempdirs):

current_chunk = list(islice(input_iterator,buffer_size))

if not current_chunk:

break

current_chunk.sort(key=key)

output_chunk = open(os.path.join(tempdir,'%06i'%len(chunks)),'w+b',64*1024)

chunks.append(output_chunk)

output_chunk.writelines(current_chunk)

output_chunk.flush()

output_chunk.seek(0)

with open(output,'wb',64*1024) as output_file:

output_file.writelines(merge(key, *chunks))

finally:

for chunk in chunks:

try:

chunk.close()

os.remove(chunk.name)

except Exception:

pass

错误跟踪:Traceback (most recent call last):

File "./batch_sort.py", line 108, in

batch_sort(args[0],args[1],options.key,options.buffer_size,options.tempdirs)

File "./batch_sort.py", line 54, in batch_sort

output_file.writelines(merge(key, *chunks))

File "./batch_sort.py", line 30, in merge

yield element.obj

AttributeError: 'str' object has no attribute 'obj'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,可以使用Python内置的`sorted`函数和`os`模块来实现文件信息排序,具体步骤如下: 1. 使用`os.listdir`函数获取目标文件夹中所有文件文件名,并保存在一个列表中。 2. 遍历列表中的每个文件名,使用`os.path.join`函数将文件名与目标文件夹路径拼接,获取文件的完整路径。 3. 使用`os.path.getmtime`函数获取文件的最后修改时间,并将该时间作为元组的第一个元素。 4. 将文件名和修改时间元组作为一个元素添加到另一个列表中。 5. 使用`sorted`函数对文件名和修改时间元组的列表进行排序排序的关键字为元组的第一个元素(即修改时间)。 6. 遍历排序后的列表,输出排序后的文件名和修改时间信息。 下面是示例代码: ```python import os folder_path = "your/folder/path" # 目标文件夹路径 # 获取目标文件夹中所有文件文件名 file_names = os.listdir(folder_path) # 遍历所有文件,获取文件名和修改时间信息 file_info = [] for name in file_names: file_path = os.path.join(folder_path, name) # 文件的完整路径 mtime = os.path.getmtime(file_path) # 文件的最后修改时间 file_info.append((name, mtime)) # 将文件名和修改时间元组作为一个元素添加到列表中 # 按照修改时间排序文件信息列表 sorted_file_info = sorted(file_info, key=lambda x: x[1]) # 输出排序后的文件名和修改时间信息 for name, mtime in sorted_file_info: print(f"{name} - {mtime}") ``` 请将`your/folder/path`替换为目标文件夹的实际路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值