I have a project that i wrote with OOP on separated modules. First module that gets imported and called by an object is "DataLibrary".
- DataLibrary module's data source is an .xlsx file which contains a table.
- I use pandas.read_excel to read it and .to_csv to convert it into a .csv file.
- Then i use pandas.read_csv to use the file in pandas (filtering columns, getting rows etc.)
To make sure that .csv file exists in working directory i use try-except-else statements. They do these steps i wrote above.
import pandas
import os.path
class DataLibrary:
def __init__(self):
try:
os.path.isfile("iata-codes.csv")
except FileNotFoundError:
self.data_xlsx = pandas.read_excel("iata-codes.xlsx", "Sheet1", dtype=str, index_col=None)
self.data_xlsx.to_csv("iata-codes.csv", encoding="utf-8", index=False)
self.df = pandas.read_csv("iata-codes.csv")
else:
self.df = pandas.read_csv("iata-codes.csv")
The issue is, even though i know that .csv file does not exist at working directory, try statement returns true which skips except statement(where .csv file supposed to be created at) and jumps into else statement where it fails on pandas.read_csv because like i said, there is no such .csv file exists at working directory.
By the way, i already checked the working directory of DataLibrary with os.getcwd and it is my project's directory. So i didn't use relative/absolute file path in my code because .py .xlsx and .csv files are all at the same folder.
I believe it is due to this os.path.isfile statement. When i searched about it, i saw that even though the file does not exist, it returns True if the file is already open in memory. I did use this .csv file before in a different project folder and i didn't use "with" statement to close it after use. Maybe i should clear my IDE's currently open files memory to solve this problem. Is there any way to do that?
This is a related answer i found, but it doesn't explain what to do if file is still open in memory : https://stackoverflow.com/a/76055876/23261968
Try something like this: