Close file object opened and never closed

Python pattern

We should close the file object opened without corresponding close.

File object opened never closed

BEFORE
def func1():
    # BAD: open-never-closed
    fd = open('foo')
    x = 123
    
def func1():
    # BAD: open-never-closed
    fdd = tarfile.open('foo')
    x = 123    
  
def func1():
    # BAD: open-never-closed
    fd22 = tempfile.SpooledTemporaryFile('foo')
    x = 123 


def test4():
    # GOOD: close-not-needed
    with open("/tmp/blah.txt", 'r') as fin:
        data = fin.read()       

def test4():
    # GOOD: close-not-needed
    with io.open("/tmp/blah.txt", 'r') as fin:
        data = fin.read()    

def test4():
    # GOOD: close-not-needed
    with empfile.TemporaryFile("/tmp/blah.txt", 'r') as fin:
        data = fin.read()    



def func2():
    # GOOD:open-never-closed
    fd = open('bar')
    fd.close()

def func3():
    # GOOD: open-never-closed
    fd = open('baz')
    try:
        pass
    finally:
        fd.close()
AFTER
def func1():
    # BAD: open-never-closed
    fd = open('foo')
    x = 123 
    fd.close()
    
def func1():
    # BAD: open-never-closed
    fdd = tarfile.open('foo')
    x = 123 
    fdd.close()    
  
def func1():
    # BAD: open-never-closed
    fd22 = tempfile.SpooledTemporaryFile('foo')
    x = 123 
    fd22.close() 


def test4():
    # GOOD: close-not-needed
    with open("/tmp/blah.txt", 'r') as fin:
        data = fin.read()       

def test4():
    # GOOD: close-not-needed
    with io.open("/tmp/blah.txt", 'r') as fin:
        data = fin.read()    

def test4():
    # GOOD: close-not-needed
    with empfile.TemporaryFile("/tmp/blah.txt", 'r') as fin:
        data = fin.read()    



def func2():
    # GOOD:open-never-closed
    fd = open('bar')
    fd.close()

def func3():
    # GOOD: open-never-closed
    fd = open('baz')
    try:
        pass
    finally:
        fd.close()