我创建了这个小程序来搜索一个目录中的所有PDF,确定它们是否可搜索,然后将它们移动到适当的目录。你知道吗
我是Python新手,这可能不是最好的方法,但它确实可以工作,直到文件名中有空格,并且返回以下内容。你知道吗
任何帮助都将不胜感激。你知道吗
>>> os.system("pdffonts.exe " + pdfFile + "> output.txt")
99
import os
import glob
import shutil
directory = os.chdir("C:\MyDir") # Change working directory
fileDir = glob.glob('*.pdf') # Create a list of all PDF's in declared directory
numFiles = len(fileDir) # Lenght of list
startFile = 0 # Counter variable
seekWord = "TrueType"
while startFile < numFiles:
pdfFile=fileDir[startFile]
os.system("pdffonts.exe " + pdfFile + "> output.txt")
file1output = open("output.txt","r")
fileContent = file1output.read()
if seekWord in fileContent:
shutil.move(pdfFile , "NO_OCR")
else: shutil.move(pdfFile, "OCR")
startFile = startFile + 1
问题似乎不是来自python,而是Windows shell。你需要用引号括起来。因为我没有你的程序pdffonts.exe,我无法调试。我还让你的代码更像Python
os.system()
使用shell执行命令。您必须引用您的文件名,shell才能将空格识别为文件的一部分,您可以使用^{但是,根本没有理由使用} function 并将其配置为在不使用重定向或shell的情况下传回输出:
os.system()
和shell。您应该使用^{因为
pdfFile
直接传递给pdffonts.exe
,所以不需要担心shell解析,空格也不再重要。你知道吗注意,我将
seekWord
改为bytes
文字,因为result.stdout
是字节值(这里不需要尝试将结果解码为Unicode)。你知道吗相关问题 更多 >
编程相关推荐