Try to search your question here, if you can't find : Ask Any Question Now ?

How to open a file without pointing it's path?

HomeCategory: stackoverflowHow to open a file without pointing it's path?
Avatarrupesh asked 2 weeks ago

I’m creating Hangman game using classes that are in separate files.
One of them ( responsible for randomly selecting a word from text file, and it’s in a package “words_from_file”.
Later on I’m importing this module to another file containing class with the main game ( However it’s in a different folder.

Please see visual explanation ->

So far I tried with os module and adding

open(os.path.dirname("words.txt"), "r")

However it raises FileNotFoundError: [Errno 2] No such file or directory: ”

Currently I’m pointing path of package containing text file in in the following way :

file = open('words_from_file/words.txt', 'r')

init file in my words_from_file package contains :

from .wordsHangman import Words
__all__ = ["Words"]   

I’m importing wordsHangman module to hangman with :

from words_from_file import *

I would like to be able to import this module without ‘manually’ specifying file’s path (so if it will be moved to another package, my hangman module will still be able to import it).

How can I do this ?

1 Answers
Best Answer
AvatarArben answered 2 weeks ago
Your Answer

17 + 3 =

Popular Tags

WP Facebook Auto Publish Powered By :