Jul-13-2023, 06:47 PM
(This post was last modified: Jul-13-2023, 06:47 PM by deanhystad.)
Your problem has nothing to do with IDLE. Your program cannot open the words file because you did not provide the correct path in the Python program. Running your program from the cmd shell would give you the same error message. All your fault. Don't blame IDLE, or Python. Programming is a humiliating occupation. That is the price we pay for getting to do something that can be so fun and rewarding.
"C:/Users/hp/Desktop/words.txt" is a bad place to put the words file. If you are writing your program in "C:/Users/hp/Desktop/Pythoncodes/', the words file should be in the same folder. Your program needs this file to work. It is almost like the file is part of the code. Put the program and the resources that the program needs in the same place.
Once you have the words.txt file in the same folder as your "random word finder.py" program, you don't need to provide the full path to the words file. Unless you know, with certainty, that a file will always be in the same location, you are better off using a relative path. Using relative paths lets you move your program, and resources, to a different folder without breaking the program.
This version of your code uses the pathlib library to look for the words.txt file in the same folder as your program file. __file__ is the full path of your program file. Path(__file__).parent gets the folder for the program file. Path(__file__).parent / "words.txt" is the full path to your words.txt file in the same folder as the program file.
"C:/Users/hp/Desktop/words.txt" is a bad place to put the words file. If you are writing your program in "C:/Users/hp/Desktop/Pythoncodes/', the words file should be in the same folder. Your program needs this file to work. It is almost like the file is part of the code. Put the program and the resources that the program needs in the same place.
Once you have the words.txt file in the same folder as your "random word finder.py" program, you don't need to provide the full path to the words file. Unless you know, with certainty, that a file will always be in the same location, you are better off using a relative path. Using relative paths lets you move your program, and resources, to a different folder without breaking the program.
This version of your code uses the pathlib library to look for the words.txt file in the same folder as your program file. __file__ is the full path of your program file. Path(__file__).parent gets the folder for the program file. Path(__file__).parent / "words.txt" is the full path to your words.txt file in the same folder as the program file.
import random from pathlib import Path # Load words file. with open(Path(__file__).parent / "words.txt", "r") as f: word_list = [word.strip() for word in f.readlines()] size = int(len(word_list) / 1000) print( ">>>>>>>>>>Random Word Finder<<<<<<<<<<", "", f"Using a {size}K english word text file I can pick any words at random", "", sep="\n", ) count = int(input("How many words shall I choose? ")) print( "--------------------", *random.sample(word_list, count), "--------------------", sep="\n", )