Python - [Errno 2] There is no such file or directory,

I am trying to make a minor modification to the python script made by my predecessor, and I ran into a problem. I studied programming, but coding is not my profession.

The python script processes SQL queries and writes them to an excel file, there is a folder in which all queries are stored in .txt format. The script creates a list of queries found in the folder and passes them one by one in a for loop.

My problem is that if I want to rename or add a request to a folder, I get the error message "[Errno 2] No such file or directory". The script uses a relative path, so I am puzzled why it continues to make errors for non-existent files.

queries_pathNIC = "./queriesNIC/"

def queriesDirer():
    global filelist
    l = 0
    filelist = []
    for file in os.listdir(queries_pathNIC):
        if file.endswith(".txt"):
            l+=1
            filelist.append(file)
    return(l)

If the problem occurs in the main function:

for round in range(0,queriesDirer()):
    print ("\nQuery :",filelist[round])
    file_query = open(queries_pathNIC+filelist[round],'r'); # problem on this line
    file_query = str(file_query.read())

Contents of the queriesNIC folder

  • 00_1_Hardware_WelcomeNew.txt
  • 00_2_Software_WelcomeNew.txt
  • 00_3_Software_WelcomeNew_AUTORENEW.txt

, "00_1_Hardware_WelcomeNew_sth.txt" - , :

FileNotFoundError: [Errno 2] No such file or directory: './queriesNIC/00_1_Hardware_WelcomeNew.txt'

(: "00_1_Hardware_Other.txt" ), script , , .

Python 3.4.

- , ?

+4
1

. glob , .txt, .

import glob, os

queries_pathNIC = "./queriesNIC/"

def queriesDirer(directory):
    return glob.glob(os.path.join(directory, "*.txt"))

for file_name in queriesDirer(queries_pathNIC):
    print ("Query :", file_name)

    with open(file_name, 'r') as f_query:
        file_query = f_query.read()

, , , round .

+1

All Articles