Q:

Unable to delete some csv file conditionally

I've written a script in python to get some tabular content from a webpage and write the same in a csv file. What I wish to do now is let my script write the content in a csv file only if the table (visible as Top Mutual Fund Holders) is available in that page otherwise it will remove the csv file that has been created. 
The table is avaible in this webpage.
The table I'm looking for is not available in this webpage.
This is my try:
import os
import csv
import requests
from bs4 import BeautifulSoup

url = "https://finance.yahoo.com/quote/UBER/holders?p=UBER"

def get_mutual_fund(soup):
    datalist = []
    for items in soup.select_one("h3:contains('Top Mutual Fund Holders')").find_next_sibling().select("table tr"):
        data = [item.text for item in items.select("th,td")]
        datalist.append(data)
    return datalist

def get_records(link):
    r = requests.get(link)
    soup_obj = BeautifulSoup(r.text,"lxml")
    try:
        item_one = get_mutual_fund(soup_obj)
    except AttributeError: 
        item_one = ""

    if item_one:
        writer.writerows(item_one)
    else:
        os.remove("mutual_fund.csv")
    return item_one

if __name__ == '__main__':
    with open("mutual_fund.csv","w",newline="") as f:
        writer = csv.writer(f)
        for elem in get_records(url):
            print(elem)

I've tried with the link that do not have that table. However, it throws the following error 
while deleting the csv file:
Traceback (most recent call last):
  File "C:\Users\WCS\AppData\Local\Programs\Python\Python37-32\demo.py", line 33, in <module>
    for elem in get_records(url):
  File "C:\Users\WCS\AppData\Local\Programs\Python\Python37-32\demo.py", line 27, in get_records
    os.remove("mutual_fund.csv")
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'mutual_fund.csv'

How can I delete the csv file when the tabular content is not present?

A:

If you keep your existing logic as it is and delete the file when the content within csv is nothing then the following should work:
import os
import csv
import requests
from bs4 import BeautifulSoup

# url = "https://finance.yahoo.com/quote/fb/holders?p=FB"
url = "https://finance.yahoo.com/quote/UBER/holders?p=UBER"

def get_mutual_fund(soup):
    datalist = []
    for items in soup.select_one("h3:contains('Top Mutual Fund Holders')").find_next_sibling().select("table tr"):
        data = [item.text for item in items.select("th,td")]
        datalist.append(data)
    return datalist

def get_records(link):
    r = requests.get(link)
    soup_obj = BeautifulSoup(r.text,"lxml")
    try:
        item_one = get_mutual_fund(soup_obj)
    except AttributeError: 
        item_one = ""

    if item_one:
        writer.writerows(item_one)
    else:
        f.close()
        os.remove('mutual_fund.csv')

if __name__ == '__main__':
    with open("mutual_fund.csv","w",newline="") as f:
        writer = csv.writer(f)
        get_records(url)

