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
#1
IDLE can not open/ file directory even when the file existed on the directory.
Disk Location: C:\Users\hp\Desktop\Pythoncodes
Word text download from GitHub.

Result:

Output:
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. = RESTART: C:/Users/hp/Desktop/Pythoncodes/random word finder.py >>>>>>>>>>Random Word Finder<<<<<<<<<<
Using a 466K english word text file I can pick any words at random.


How many words shall I choose?20
Error:
Traceback (most recent call last): File "C:/Users/hp/Desktop/Pythoncodes/random word finder.py", line 8, in <module> with open('C:/Users/hp/Desktop/Pythoncodes/word.text', "rt") as f: FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/hp/Desktop/Pythoncodes/word.text'
code:

import random

print(">>>>>>>>>>Random Word Finder<<<<<<<<<<")
print("\nUsing a 466K english word text file I can pick any words at random.\n")

wds=int(input("\nHow many words shall I choose?"))

with open('C:/Users/hp/Desktop/Pythoncodes/word.text', "rt") as f:
    words = f.readlines()
    words = [w.rstrip() for w in words]

print("--------------------")

for w in random.sample(words, wds):
    print (w)
print("--------------------")
Larz60+ write Jul-13-2023, 09:48 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fiixed this time. Please use BBCode tags on future posts.
Reply
#2
word.txt?
Reply
#3
>>>>>>>>>>Random Word Finder<<<<<<<<<<

Using a 466K english word text file I can pick any words at random.


How many words shall I choose?12
Traceback (most recent call last):
File "C:/Users/hp/Desktop/Pythoncodes/randomwords2.py", line 8, in <module>
with open('C:/Users/hp/Desktop/Pythoncodes/words.text', "rt") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/hp/Desktop/Pythoncodes/words.text'

the text file was download from git hub in text format. Titled "Words".
Reply
#4
I am still going to guess that the error is a typo in the path, the filename, or the extension. Text files usually have the extension .txt, not .text. Mabye there is no extension at all. So what is the name you see when you look at the folder?
Reply
#5
The code is to open a file name "words" located in the hard disc, with file path:
1.C:\Users\hp\Desktop\words.txt

2.with this command
3.with open("/home/pi/Downloads/words.txt", "rt") as f:
4. words = f.readlines()
5.words = [w.rstrip() for w in words]

#question:
how do I insert the file location on line 1 to line 3 so it can be opened by phyton IDLE.

complete code;
import random
print(">>>>>>>>>>Random Word Finder<<<<<<<<<<")
print ("\nUsing a 466k English word text file i can pick any words at random.\n")

wds=int(input("\nHow many words shall i choose?"))

with open("/home/pi/Downloads/words.txt", "rt") as f:
       words = f.readlines()
words = [w.rstrip() for w in words]

print("--------------------")

for w in random.sample(words, wds):
        print(w)

print("--------------------")

Using Chat GPT, 
To modify the code to open the "words" file located at C:\Users\hp\Desktop\words.txt using Python IDLE, you need to update the file path in line 3. Here's the modified code:
import random

print(">>>>>>>>>> Random Word Finder <<<<<<<<<<")
print("\nUsing a 466k English word text file, I can pick any words at random.\n")

wds = int(input("\nHow many words shall I choose? "))

# Update the file path to C:\Users\hp\Desktop\words.txt
with open(r"C:\Users\hp\Desktop\words.txt", "rt") as f:
    words = f.readlines()
words = [w.rstrip() for w in words]

print("--------------------")

for w in random.sample(words, wds):
    print(w)

print("--------------------")
buran write Jul-13-2023, 08:44 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#6
/home/pi/Downloads/words.txt is Linux/Mac path
C:\Users\hp\Desktop\words.txt is Windows

1. Using raw string to handle back-slash correctly
with open(r"C:\Users\hp\Desktop\words.txt", "rt") as f:
or

escaping the back-slash
with open("C:\\Users\\hp\\Desktop\\words.txt", "rt") as f:
or
using forward slash
with open("C:/Users/hp/Desktop/words.txt", "rt") as f:

And this was already covered in your first thread on the forum

https://python-forum.io/thread-39705.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error. xflyerwdavis 2 532 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  Coding Error EddieG 2 549 Jul-09-2023, 02:59 AM
Last Post: EddieG
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,393 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,603 Oct-28-2021, 09:10 AM
Last Post: pyhill00
  Error about missing directory Led_Zeppelin 3 2,722 Aug-31-2021, 01:37 PM
Last Post: snippsat
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 6,307 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,716 Dec-11-2020, 07:07 PM
Last Post: tester_V
  python coding error isntitzee 1 2,215 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,739 Sep-28-2020, 07:57 AM
Last Post: buran
  coding error iro a menu Rollo 2 2,102 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