Python file problems

A file I wrote in Python on VSCode, which had previously been successful, suddenly doesn´t recognize the .txt file used. It reutrned this when I input my text file:

FileNotFoundError: [Errno 2] No such file or directory: 'romeo.txt'

I´m not sure why this is. If anyone has any insights, it would be helpful! For reference I will post the code below.

fname = input("Enter file name: ")
fh = open(fname)
fh = fh.read()

txt = fh.split()

lst = list()

for line in txt:
    if line not in lst:
        lst.append(line)
        lst.sort()
print(lst)

Can you share a screenshot of the folder containing the python file and the romeo.txt file?

What command are you using to execute the romeo.py file and what directory are you running it from?

If you try to execute a python file from a folder other than where the file is located, then any references to external files will need to be absolute, because Python will look for the external file relative to where the python file is executed.

For example, if you are currently in a folder like:

C:\home

and have romeo.py and romeo.txt in a subfolder of home called files like:

C:\home\files\romeo.py
C:\home\files\romeo.txt

If you run romeo.py like:

C:\home > python C:\home\files\romeo.py

then when you enter romeo.txt at the prompt, Python will look in C:\home folder and not the C:\homes\files folder for romeo.txt and cause this error.

FYI - A significantly more efficient way to accomplish this same task would be to use a set and then convert the set back into a list. Sets by default remove duplicate values.

fname = input("Enter file name: ")
fh = open(fname)
fh = fh.read()

txt_set = set(fh.split())
lst = list(txt_set)
lst.sort()

print(lst)