Home > Net >  FileNotFoundError: [Errno 2] No such file or directory (even though it gives the name of the exact f
FileNotFoundError: [Errno 2] No such file or directory (even though it gives the name of the exact f

Time:09-29

NOTE: I checked all the other locations for similar questions before posting. This problem did not exist 48 hours ago, and NOTHING has been changed.

My issue is that I am converting a PDF to TXT so I can take the information and put it into a database. The 1st script is to convert:

import PyPDF2
import os
import time

#Scan Directory

directory = 'C:\\Users\\Username\\OneDrive\\Desktop\\python'
for file in os.listdir(directory):
    if not file.endswith(".pdf"): #Skip non-PDF files
        continue
    with open(os.path.join(directory,file), 'rb') as pdfFileObj:  # Changes here
        pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
        pageObj = pdfReader.getPage(0)
        text=pageObj.extractText()
        print("Writing File "  file  " to TXT.")
        file2=open(r"C:\Users\Username\OneDrive\Desktop\python\\" file.replace('pdf','txt'),"a") #Write as TXT files
        file2.writelines(text)
        print("Done saving.")
        file2.close()

That part works fine still. It takes 3132019_4102019_1617.pdf and converts it to 3132019_4102019_1617.txt. (This will end up being used to take around 200 pdfs and covert them). The 2nd part, the one with an issue now, is stating that the file does not exist. The only issue is that in-order for it to tell me the name of the file not existing, the file itself has to exist.

The 2nd script (partial):

import os
import time
#import sys
import mysql.connector
from collections import deque
from itertools import groupby


directory = 'C:\\Users\\Username\\OneDrive\\Desktop\\python'
for file in os.listdir(directory):
    if file.endswith(".txt"): #Skip non-TXT files
        with open(file, 'r') as f:
            nameList = [line.strip() for line in f]
            transferlist = nameList
            char_list=['UPS A','PDU A1','PDU B1','CRAC 03','PDU A2','CRAC 05','UPS B',
            'PDU B2','CRAC 06','CRAC 07','PDU A4','DC CNC','ATS 1','ATS 2','CRAC 02','CRAC 04','CRAC 01',
            'CRAC 09','CRAC 08','CRAC 10','CRAC 11','ATS','DC A1','DC B1','PDU A4''a','b','c','d','e',
            'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
            'w','x','y','z','/',':']
            transferlist2=[ele for ele in transferlist if all(ch not in ele for ch in char_list)]

  File "c:\Users\Username\OneDrive\Desktop\python\CLT4_SQL_Insert_3132019_4102019_1617.py", line 12, in <module>
    with open(file, 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: '3132019_4102019_1617.txt'

If I remove the .txt file then the script just runs and closes without doing anything, no errors. I don't ever call which .txt file to look for as their will be many and it is supposed to do all of them. The fact that it specifically can grab the name of the .txt file means it does exist, unlike what it states. It worked flawlessly 48 hours ago and everything is located in the exact same folder.

CodePudding user response:

Instead of

open(file, 'r')

you probably want

open(os.path.join(directory, file), "r")
  • Related