#๐ Learning how to have python open files from the same folder
40 messages ยท Page 1 of 1 (latest)
@north gust
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.
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.
u want it to open like a window?
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()
cant it be if you in same directory u can do `with open("name.txt")
no, that's relative to the cwd, not the script itself
forgive my rookieness, cwd?
current working directory
xddd
e.g. if you open a terminal in windows, this part
PS C:\Users\User> py ./my_folder/script.py
^^^^^^^^^^^^^
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
__file__ is a special str variable that contains the path to the current script
Path is a nice class in pathlib for path operations
Path.parent finds the immediate parent of a Path, in the case of Path(__file__), that'd be the parent of the script, a.k.a. the folder that the script is in
and then Path() / 'something' 'joins' the paths together basically, so
p = Path('directory')
q = p / 'test.txt'
````q` is now `directory/test.txt`
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")
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.
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`
pretty much
well you can just try to call the script from a different cwd and see if it still works
# 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.
so replace the long absolute path with the pathlib stuff
and you should be fine and dandy
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
not like that
- you need to import
Pathfrompathlib - the code I showed you was an example, you should not copy it as it won't work, instead adapt it to your situation
okay no problem I can adjust it (i feel dumb)
xd coding is hard
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
sure
content = file.read() this part's extraneous though
!solved
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.