如何同时将python代码应用于文件夹中的所有文件,以及如何为每个后续输出文件创建一个新名称?

2024-04-27 19:45:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理的代码接受一个.pdf文件,并输出一个.txt文件。我的问题是,如何创建一个循环(可能是一个for循环),在文件夹中以“.pdf”结尾的所有文件上反复运行代码?此外,如何在每次循环运行时更改输出,以便每次都能编写一个与输入文件同名的新文件(例如1_宠物.pdf>;1_宠物.txt,2_宠物.pdf>;2个_宠物.txt等等)

以下是目前为止的代码:

path="2_pet.pdf"
content = getPDFContent(path)
encoded = content.encode("utf-8")
text_file = open("Output.txt", "w")
text_file.write(encoded)
text_file.close()

Tags: 文件path代码textgttxt文件夹宠物
3条回答

以下脚本解决了您的问题:

import os

sourcedir = 'pdfdir'

dl = os.listdir('pdfdir')

for f in dl:
    fs = f.split(".")
    if fs[1] == "pdf":
        path_in = os.path.join(dl,f)
        content = getPDFContent(path_in)
        encoded = content.encode("utf-8")
        path_out = os.path.join(dl,fs[0] + ".txt")
        text_file = open(path_out, 'w')
        text_file.write(encoded)
        text_file.close()

对目录中所有PDF文件进行操作的一种方法是调用glob.glob()并对结果进行迭代:

import glob
for path in glob.glob('*.pdf')
    content = getPDFContent(path)
    encoded = content.encode("utf-8")
    text_file = open("Output.txt", "w")
    text_file.write(encoded)
    text_file.close()

另一种方法是允许用户指定文件:

^{pr2}$

然后用户像python foo.py *.pdf一样运行脚本。在

创建一个函数来封装对每个文件的操作。在

import os.path

def parse_pdf(filename):
    "Parse a pdf into text"
    content = getPDFContent(filename)
    encoded = content.encode("utf-8")
    ## split of the pdf extension to add .txt instead.
    (root, _) = os.path.splitext(filename)
    text_file = open(root + ".txt", "w")
    text_file.write(encoded)
    text_file.close()

然后将此函数应用于文件名列表,如下所示:

^{pr2}$

相关问题 更多 >