4

I am trying to make a minor modification to a python script made by my predecessor and I have bumped into a problem. I have 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 where all the queries are kept in .txt format. The script creates a list of the queries found in the folder and goes through them one by one in a for cycle.

My problem is if I want to rename or add a query in the folder, I get a "[Errno 2] No such file or directory" error. The script uses relative path so I am puzzled why does it keep making errors for non-existing 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)

Where the problem arises 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 queriesNIC folder

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

The scripts runs without a problem, but if I change the first query name to "00_1_Hardware_WelcomeNew_sth.txt" or anything different, I get the following error message:

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

I have also tried adding new text files to the folder (example: "00_1_Hardware_Other.txt") and the script simply skips processing the ones I added altogether and only goes with the original files.

I am using Python 3.4.

Does anyone have any suggestions what might be the problem?

Thank you

3
  • 1
    See e.g. stackoverflow.com/q/1270951/3001761 Commented Aug 25, 2015 at 12:25
  • Which OS you are using? Commented Aug 25, 2015 at 12:29
  • How is this script run? Are there other parts? Because filelist is global, it might well be that it is modified somewhere else or even saved between runs to avoid re-processing already processed files. Commented Aug 25, 2015 at 12:35

1 Answer 1

1

The following approach would be an improvement. The glob module can produce a list of files ending with .txt quite easily without needing to create a list.

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()

From the sample you have given, it is not clear if you need further access to the round variable or the file list.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.