python fehler?

hallo, ich programmiere gerade ein tool, wenn ich eine .txt öffnen will kommt ein fehler(obwohl sie existier)

fehler:

enter sites file: sites.txt

Traceback (most recent call last):

 File “c:\Users\******\Desktop\smoothie dork\smootie.py”, line 36, in <module>

  sites = read_lines(site_file)

      ^^^^^^^^^^^^^^^^^^^^^

 File “c:\Users\*****\Desktop\smoothie dork\smootie.py”, line 14, in read_lines

  with open(file_name, “r”) as f:

     ^^^^^^^^^^^^^^^^^^^^

FileNotFoundError: [Errno 2] No such file or directory: ‘sites.txt’

code:

def read_lines(file_name):

    with open(file_name, “r”) as f:

        lines = f.readlines()

    return lines

print(colorama.Fore.LIGHTMAGENTA_EX, logo)

site_file = input(“enter sites file: “)

sites = read_lines(site_file)

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
4 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
KarlRanseierIII
7 months ago

Ohen PFadangabe is trying to open the file in the current work directory, which is typically the same directory in which the script is located.

So if the file is found nciht, then it is not where it is searched and where it is, it would simply be out:

import os

....
   print(os.getcwd())
ohwehohach
7 months ago

Well, apparently the file doesn’t exist, otherwise you wouldn’t get the mistake. A common error is that files are always found in the same folder. This is true if the script directory is also the work directory, but this is often not the case.

Let either output the current path (you can see what Python thinks what the work directory is) or enter the full path when opening the file.

ohwehohach
7 months ago
Reply to  philipDirtJump

You have to understand what I’m writing. I’m not saying the file doesn’t exist. I say she doesn’t exist in the place where Python is looking for it!

If you only add the file name, Python will search in the current work directory. And that can be another than the directory in which the script is.

So give the complete path – then the mistake should go away.