0

I have a csv file, I'm reading its values after end of each script. After around 20mins I get an error OSError: {Errno 24] Too many open files: "file path pointing to csv file"

tried setting ulimit -Sn to 20000

with open(path) as id_list:
    csv_reader = csv.DictReader(id_list, delimiter=',')
    for i in csv_reader:
        if i['TestScript'] == filename:
            return (i['TestID'])
    return ("Assign ID For " + filename)
  • 2
    This code only opens one file; if it's the source of your problem then it must be in a loop or a function/method called more than once. Please show us enough code to demonstrate/reproduce the problem. https://stackoverflow.com/help/minimal-reproducible-example – kaya3 Nov 11 '19 at 12:01

1 Answers1

0

(Edit:) your code should actually run fine; however you will have to e.g. call it in a function like

def check(path, filename):
    with open(path) as id_list:
        csv_reader = csv.DictReader(id_list, delimiter=',')
        for i in csv_reader:
            if i['TestScript'] == filename:
                return i['TestID']
    return f"Assign ID For {filename}"

you could now call the check function e.g. for each path in a list of paths etc.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • *"if you return inside the with statement, it won't close the file handle as you expect."* I don't think this is true; see https://stackoverflow.com/questions/9885217/in-python-if-i-return-inside-a-with-block-will-the-file-still-close – kaya3 Nov 11 '19 at 11:58
  • @kaya3, thanks for the clarification! I suspect you are right with your comment under the question, pointing to some other issue not related to `with` not closing properly... – FObersteiner Nov 11 '19 at 12:32