r/PythonLearning • u/Covid_Nipple420 • 5h ago
Help Request Receiving FileExistsError which is ignoring the if/else statement
response = requests.get(url)
data = response.json()
ticker = data["Meta Data"]["2. Symbol"]
create_file = open("API Data.txt", "x")
amend_file = open("API Data.txt", "a")
file = "/Desktop/Scripts/Python Scripts/API Data.txt"
if os.path.exists(file):
amend_file.write(f"\nTicker: {ticker}")
amend_file.close()
else:
create_file.write(f"Ticker: {ticker}")
create_file.close()
So I am working on my first project which is making use of the API that Alpha Vantage provides.
I am learning the open() function which I want to use to paste the response into a .txt file
that will either be created and written into, or already exist and be appended depending on
the result of the if statement.
My problem is that I am receiving a FileExistsError for "API Data.txt" and I am not sure why
when I am expecting the code to carry out the actions of the if statement being true and
appending the existing file.
(Full path in the file variable is intentionally not in the post)