0

My code was working until yesterday, January 31, 2022. But today, Feb. 1, 2022, it sudden

Here's the error:

Traceback (most recent call last): File "/Users/folder/code/3.cut_PDF.py", line 19, in onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))] NotADirectoryError: [Errno 20] Not a directory: '/Users/.../Riskalyze/02.01.22/.DS_Store'

Here's my code:

import PyPDF2
from PyPDF2 import PdfFileWriter
from os import listdir
from os.path import isfile, join
import pandas as pd
from datetime import date

today_date = date.today().strftime("%m.%d.%y")
print(today_date)

mypath_folder = '/Users/.../Riskalyze/' + today_date + '/'

onlyfolder = [f for f in listdir(mypath_folder)]
print(onlyfolder)

pdfWriter = PdfFileWriter()
for folder in onlyfolder:
    mypath = mypath_folder + folder
    onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
    print(onlyfiles)
    i = 0
    if folder == "#Model":
        model_name = []
        print(type(model_name))
        for file in onlyfiles:
            file_name = file.rsplit(".")[0]
            file_name = file_name.split("Portfolio")[0]
            file_name = file_name + "Portfolio"
            model_name.append(file_name)
            print("model name:", model_name)
    else:
        pass
    for file in onlyfiles:
        print(file)
        mypath_new = mypath + '/' + file
        print(mypath_new)
        pdfFileObj = open(mypath_new, 'rb')
        pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
        pageObj = pdfReader.getPage(0)
        pdfWriter.addPage(pageObj)
        i += 1
        print("i:", i)

pdfOutputFile = open('/Users/.../Riskalyze/MergedFiles/MergedFiles_' + today_date + '.pdf', 'wb')
pdfWriter.write(pdfOutputFile)
pdfFileObj.close()
pdfOutputFile.close()
print(model_name)

1 Answers1

0

(pathlib from the standard Python library is much more handy to use than os and os.path, I suggest you give it a try)

As for your problem,

onlyfolder = [f for f in listdir(mypath_folder)]

is wrong.

The doc for os.listdir :

Return a list containing the names of the entries in the directory given by path.

It lists the content of a directory, not just the directories in it. So you have a filename in your onlyfolder list, so that later when you do isfile(join(mypath, f)) it tries to access f (the file name) in the mypath directory, which happens to be a file (.DS_Store).

You could have found it yourself by using a debugger or simply printf-debugging.

Lenormju
  • 4,078
  • 2
  • 8
  • 22
  • 1
    The issue was resolved by updating the chromedriver. I will definitely look into pathlib. Thank you so much for your help! – itsmenick212 Feb 02 '22 at 23:30