#๐Ÿ”’ Learning how to have python open files from the same folder

40 messages ยท Page 1 of 1 (latest)

grim sonnetBOT
#

@north gust

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

north gust
#

In my project I want python to look for my sevenletterwords.txt file by looking in the same directory. I had an issue with my visual studio code debugger and someone helped me set python to look for an absolute path. But an absolute path will not work for everyone as not everyone can operate my computer.

I want my python program to recognize the sevenletterwords.txt file and access its contents.

tired cobalt
#

u want it to open like a window?

winged lion
#

a clean way is through pathlib

from pathlib import Path

folder = Path(__file__).parent
with open(folder / 'text_in_same_folder.txt') as f:
    content = f.read()
tired cobalt
#

cant it be if you in same directory u can do `with open("name.txt")

winged lion
north gust
#

forgive my rookieness, cwd?

winged lion
tired cobalt
#

xddd

winged lion
#

e.g. if you open a terminal in windows, this part

PS C:\Users\User> py ./my_folder/script.py
   ^^^^^^^^^^^^^
north gust
#

so Path is a function of path library and you use the file method of it to find the file. and than .parent what is that part

winged lion
#

and then Path() / 'something' 'joins' the paths together basically, so

p = Path('directory')
q = p / 'test.txt'
````q` is now `directory/test.txt`
tired cobalt
#

yes

#

but why again mine doesnt work

#

if u dont specify path or folder doesnt it take file from that folder u in?

#

like with open("name.txt","r")

north gust
#

Okay so I need to make the changes and than I need to test if it works on someone elses computer. If working correctly the user will be able to run the python program on their computer because python will be able to see the program on its own.

winged lion
# tired cobalt like `with open("name.txt","r")`

again no, it's relative to the cwd
example:

PS C:\Users\User> py ./my_folder/script.py
```then you have
```py
# script.py
with open('test.txt') as f:
    ...
```it'll try to open `C:\Users\User\test.txt`
tired cobalt
#

ohhh

#

okay

winged lion
north gust
#
# this function will gather in the game words from the sevenletterwords.txt file and be returned in this function.
def get_word_list():
    with open(
        r"C:\Users\Andrew\Desktop\Main_Desktop_Folder\programming\python_projects\python_hacking_game\sevenletterwords.txt",
        "r",
    ) as file:
        word_list = [line.strip().upper() for line in file.readlines()]
        random.shuffle(word_list)
        # test print(full_list_of_words)
    return word_list


from pathlib import Path

folder = Path(__file__).parent
with open(folder / 'text_in_same_folder.txt') as f:
    content = f.read()
#

above is the get_word_list function.

I have your code just below it for the time being.

#

So once it is read I have some other functionality where the word_list is cleaned up and returned.

winged lion
#

so replace the long absolute path with the pathlib stuff
and you should be fine and dandy

north gust
#

okay so just switch out the top half gotcha

#
# this function will gather in the game words from the sevenletterwords.txt file and be returned in this function.
def get_word_list():
    with open(
        folder = Path(__file__).parent
        with open(folder / 'text_in_same_folder.txt') as f:
        content = f.read()
        
        # r"C:\Users\Andrew\Desktop\Main_Desktop_Folder\programming\python_projects\python_hacking_game\sevenletterwords.txt",
        # "r",
    ) as file:
        word_list = [line.strip().upper() for line in file.readlines()]
        random.shuffle(word_list)
        # test print(full_list_of_words)
    return word_list

just coming into some undefined issues

#

folder is not being recognized

#

yellow squiggly lines

winged lion
north gust
#

okay no problem I can adjust it (i feel dumb)

tired cobalt
#

xd coding is hard

north gust
# winged lion and then `Path() / 'something'` 'joins' the paths together basically, so ```py p...

am i on the right track?

from pathlib import Path
def get_word_list():
    folder = Path(__file__).parent 
    with open(folder / 'sevenletterwords.txt') as file:
        content = file.read()
        word_list = [line.strip().upper() for line in file.readlines()]
        random.shuffle(word_list)
    
        # r"C:\Users\Andrew\Desktop\Main_Desktop_Folder\programming\python_projects\python_hacking_game\sevenletterwords.txt",
        # "r",

    return word_list
winged lion
north gust
#

!solved

grim sonnetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.