使用os.walk在每个子目录上执行功能-Python

时间:2018-07-26 22:13:43

标签: python python-3.x os.walk

我正在一个项目中,该项目使用os.walk搜索子目录中的单个jpg图像并将其编译为pdf文档。我需要在os.walk(搜索目录)的每个子目录中创建一个pdf文档。我目前正在使用的脚本将在搜索目录中找到的每个jpg合并为一个大型pdf。是否可以使用os.walk为os.walk(搜索目录)的每个子目录创建pdf?

这是目录树的一个示例:

 <SearchDirectory>
   Roll 01
      frames
        001.jpg
        002.jpg
        003.jpg
   Roll 02
      frames
        001.jpg
        002.jpg
        003.jpg
   Roll 03
      frames
        001.jpg
        002.jpg
        003.jpg

从abarnert反馈后,这里的脚本已更正:

 import os, sys, img2pdf

 if len(sys.argv) > 1:
     SearchDirectory = sys.argv[1]
     print ("I'm looking for JPGs in ", SearchDirectory)
 else:
     print ("Please tell me the directory to look in")
     sys.exit()

 for root, dirs, files in os.walk(SearchDirectory):
     image_files = []
     for file in files:
         if ((os.path.basename(root)) == "frames") and (file.endswith(".jpg") or file.endswith(".JPG")):
             print("Discovered this jpg: ", os.path.join(root, file))
             image_files.append(os.path.join(root, file))

     if image_files:
         output_file = SearchDirectory + "\\" + (os.path.split(os.path.split(os.path.realpath(root))[0])[1]) + ".pdf"
         print ("Putting all JPGs into ", output_file)
         pdf_bytes = img2pdf.convert(image_files)
         file = open(output_file,"wb")
         file.write(pdf_bytes)
     else:
         print ("Couldn't find any JPGs")

1 个答案:

答案 0 :(得分:0)

如果要处理每个子目录的图像文件,则应将处理逻辑放入os.walk循环中。 image_files也应在每个循环中重新初始化:

import os, sys, img2pdf

if len(sys.argv) > 1:
    SearchDirectory = sys.argv[1]
    print("I'm looking for JPGs in ", SearchDirectory)
else:
    print("Please tell me the directory to look in")
    sys.exit()

for root, dirs, files in os.walk(SearchDirectory):
    image_files = []
    for file in files:
        if ((os.path.basename(root)) == "frames") and (file.endswith(".jpg") or file.endswith(".JPG")):
            print("Discovered this jpg:", os.path.join(root, file))
            image_files.append(os.path.join(root, file))
    if image_files:
        output_file = SearchDirectory + "\\" + (os.path.split(os.path.split(os.path.realpath(root))[0])[1]) + ".pdf"
        print("Putting all JPGs into", output_file)
        pdf_bytes = img2pdf.convert(image_files)
        file = open(output_file, "wb")
        file.write(pdf_bytes)
    else:
        print("Couldn't find any JPGs in", root)