I want to copy my receipts based on the text in PDF. The copy function doesn't work and gives the following error. FileNotFoundError: [Errno 2] No such file or directory
Though it prints the list of files and it is there.
************************** CODE ************************
#//set path
ls_base = r"D:\OneDrive\Documents\Receipts"
ls_t1 = r"D:\OneDrive\Documents\Receipts\Org1"
ls_t2 = r"D:\OneDrive\Documents\Receipts\Org2"
os.chdir(ls_base)
files = os.listdir(ls_base)
print(files) #PRINTS THE LIST OF FILES CORRECTLY
os.chdir(ls_base)
for files in os.listdir(ls_base):
if files.endswith(".pdf"):
pdfFileObj = open(files, 'rb')
pdfReader = PyPDF2.PdfReader(pdfFileObj)
pageObj = pdfReader.pages[0]
ls_name = re.findall(r'The Paint Shop', pageObj.extract_text())
pdfFileObj.close()
ls_file = ls_base + files //get complete file name
if ls_name:
print(ls_name, pageObj.extract_text().split('Items')[1].split('\n')[1], files)
ls_target = ls_t1 + files
shutil.copy2(ls_file, ls_target)
else:
#//copy the file to the location "D:\OneDrive\Documents\Receipt\Org2"
ls_target = ls_t2 + files
shutil.copy2(ls_file, ls_target)
I think there's "/" missing between ls_base
and files
in this line:
ls_file = ls_base + files //get complete file name
So ls_file ends up being equal to "D:\OneDrive\Documents\ReceiptsYOUR_PDF.pdf"
In general it helps to print out the names to see if you're getting what is expected.