Python Forum
Coding error. Can't open directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding error. Can't open directory
#7
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.
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",
)
Reply


Messages In This Thread
Coding error. Can't open directory - by EddieG - Jul-12-2023, 01:34 PM
RE: Coding error. Can't open directory - by EddieG - Jul-12-2023, 01:52 PM
RE: Coding error. Can't open directory - by EddieG - Jul-13-2023, 08:39 AM
RE: Coding error. Can't open directory - by buran - Jul-13-2023, 08:47 AM
RE: Coding error. Can't open directory - by deanhystad - Jul-13-2023, 06:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error. xflyerwdavis 2 550 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  Coding Error EddieG 2 564 Jul-09-2023, 02:59 AM
Last Post: EddieG
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,518 Jun-27-2023, 01:17 PM
Last Post: diver999
  Error when using Watchdog and Qt to keep open a Queue before and after sending first pyhill00 0 1,620 Oct-28-2021, 09:10 AM
Last Post: pyhill00
  Error about missing directory Led_Zeppelin 3 2,773 Aug-31-2021, 01:37 PM
Last Post: snippsat
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 6,379 Apr-05-2021, 07:12 PM
Last Post: Brian177
  List of error codes to find (and count) in all files in a directory tester_V 8 3,783 Dec-11-2020, 07:07 PM
Last Post: tester_V
  python coding error isntitzee 1 2,229 Oct-17-2020, 06:30 AM
Last Post: buran
  Coding error- Not sure where I have put error markers against the code that is wrong Username9 1 1,760 Sep-28-2020, 07:57 AM
Last Post: buran
  coding error iro a menu Rollo 2 2,123 Sep-27-2020, 04:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020