(May-28-2017, 11:56 AM)Ewkos Wrote: [ -> ]But i want to do this in foo, so i include readSpam in foo and there i call:
result = readSpam.pick()
Now i get the error:
FileNotFoundError: [Errno 2] No such file or directory: 'spam.txt'
You most give folder path to spam.txt.
Eg from my example:
# bar_1.py
def spam():
with open('main_foo/sub/car.txt') as f:
car = f.read()
return 'hello from bar_1: {}'.format(car)
So
car.txt
is in placed in sub folder together with
bar_1.py
,but have to give it whole path to main folder.
λ ptpython
>>> import main_foo
>>> main_foo.bar_2.goo()
i am bar__2 and hello from <hello from bar_1: BMW>
>>>
Hi, thanks for your explanation to init.py
Now its a little bit clearer.
Coming back to my other question:
If i do this folder path, it gives me the same error.
I think the problem is, that in your example you run this file in a folder above and in my example i run it in a folder at the same level.
So my question is, how can i do this?
Another question: I want to run this module in other modules too, but they are in different folders and subfolders; this is now problematic if i always have to use a absolute path form the running file to its location, so the only way i see is to write it again.
Quote:I think the problem is, that in your example you run this file in a folder above and in my example i run it in a folder at the same level.
car.txt is on same level.
main_foo/
|-- __init__.py
|-- foo.py
sub/
|-- __init__.py
|-- bar_1.py
|-- bar_2.py
|-- car.txt
Quote:Another question: I want to run this module in other modules too, but they are in different folders and subfolders; this is now problematic if i always have to use a absolute path form the running file to its location, so the only way i see is to write it again.
Not really,you just have to practice a little a more,when know how it work is easier design in the first place.
Likes should i have many single modules that talk to each other,
or make a class with inheritance where all work together before making a package.
You have to give absolute path inside the package system,but that dos not mean that a relative path file can come from other places.
Eg
open('main_foo/sub/{}'.format(comming from a other place))
As mention this take time to warp head around,i have struggle with this many times

Oh i think i explained it wrong, my problem is, that in your example the executive module is one level above then the file and in my example the running example is on the same level
Quote:To my above question:
I have the following Folder and file structure:
Folder A
-FolderB
--spam.txt
--read.py
--readSpam.py
-FolderC
--foo.py
There readSpam.py has a function pick() which calls read to read the txt file.
readSpam calls read to read a txt-file in a given format, so with this module i can read spam.txt
If i want to run readspam.py i just have to use the code:
fobj = open("test.txt")
Then i can open the file and read it with read.py
But i want to do this in foo, so i include readSpam in foo and there i call:
result = readSpam.pick()
Now i get the error:
FileNotFoundError: [Errno 2] No such file or directory: 'spam.txt'
But if i put spam.txt in the same folder as foo.py the code works.
So my question is, how do i have to change my code to leave spam.txt in folder B?
To be honest the setup dos not look good,just mind bending
Do you have
__init__.py
in all folder as you should?
So i guess you could try to give path to "spam.txt"
Folder A/FolderB/spam.txt
Hi, sry for the late answer:
Yeah now i have all __init__.py files.
If i gave a this path above which you mentioned, then i get still the same error:
FileNotFoundError: [Errno 2] No such file or directory: 'A/B/spam.txt'
Maybe i am structuring my programm wrong.
So the idea is to have a data folder where my data is, in this folder i have some python files to read them. Then i have another folder for my code which can work with the input given in data.
Furthermore i have a scripts folder where some scripts will be; so the problem above accours here, in this script folder i now have a script, whoch should run my aglorithms for the data given in folder data.
So my question is, is this ok if i do this like that; then the above problem has to be solved or my structure is wrong, then i have to restructure my program and hopefully this problem will vanish.
Thank You!