0

I tried opening a file and it says, FileNotFoundError: [Errno 2] No such file or directory: 'words.txt' although it is in the same directory, here is the code I am using:

words = []

with open("words.txt", "r") as file:
    words_string = file.read()
    words = words_string.split()

1 Answers1

0

Use os.listdir() to list the files in the current working directory and check if the desired file is present. To print the path of current working directory use os.getcwd(). I would recommend to use absolute paths instead of relative path to make the code clean and less prone to errors. If you don't want to use the absolute paths then use os.chdir(directory_name) to change the current working directory during runtime.

offset-null1
  • 103
  • 1
  • 2
  • 7