使用python排序_使用Python对文件列表进行排序

我需要将一个装满pdfs的文件夹合并到一个文件中.但是,它们必须按特定顺序组合.文件名的示例是:

WR_Mapbook__1.pdf

WR_Mapbook__1a.pdf

WR_Mapbook__2.pdf

WR_Mapbook__2a.pdf

WR_Mapbook__3.pdf

WR_Mapbook__3a.pdf

etc...

它们在Windows资源管理器中排序的方式是我需要将它们添加到单个文件中的方式.但是我的脚本首先添加所有“a”文件,然后添加没有“a”的文件.为什么这样做?如何对其进行排序以便以我想要的方式添加文件?

请参阅下面的代码.谢谢!

from pyPdf import PdfFileWriter, PdfFileReader

import glob

outputLoc = "K:\\test\\pdf_output\\"

output = PdfFileWriter()

pdfList = glob.glob(r"K:\test\lidar_MB_ALL\*.pdf")

pdfList.sort

print pdfList

for pdf in pdfList:

print pdf

input1 = PdfFileReader(file(pdf, "rb"))

output.addPage(input1.getPage(0))

# finally, write "output" to document-output.pdf

outputStream = file(outputLoc + "WR_Imagery_LiDar_Mapbook.pdf", "wb")

output.write(outputStream)

print ("adding " + pdf)

outputStream.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值