Python脚本对文件夹中的所有文件运行命令 [英] Python Script to run a command over all files in a folder
本文介绍了Python脚本对文件夹中的所有文件运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了将pdf转换为文本,我使用以下命令:
For converting pdf to text I am using the following command:
pdf2txt.py -o text.txt example.pdf#它将example.pdf转换为text.txt
但是我有1000多个pdf文件,我需要先将其转换为文本文件,然后再进行分析.
But I have more than 1000 pdf files which I need to convert to text file first and then do the analysis.
有没有一种方法可以使用此命令遍历pdf文件并转换所有文件?
Is there a way through which I can use this command to iterate over the pdf files and convert all of them?
推荐答案
我建议您使用shell脚本:
I would suggest you to have a shell script:
for f (*.pdf) {pdf2txt.py -o $f $f.txt}
然后使用python读取所有 .txt
文件以进行分析.
Then read all .txt
files using python for your analysis.
仅使用python进行转换:
Using only python to convert:
from subprocess import call
import glob
for pdf_file in glob.glob('*.pdf'):
call(["pdf2txt.py", "-o", pdf_file, pdf_file[:-3]+"txt"])
这篇关于Python脚本对文件夹中的所有文件运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文