Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modules in Python
#11
(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>
>>>
Reply
#12
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.
Reply
#13
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 Undecided
Reply
#14
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
Reply
#15
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 Confused
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
Reply
#16
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to see the date of installation of python modules. newbieAuggie2019 4 1,469 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  Python modules for accessing the configuration of relevant paths Imago 1 1,324 May-07-2022, 07:28 PM
Last Post: Larz60+
  Python modules to extract data from a graph? bigmit37 5 22,196 Apr-09-2021, 02:15 PM
Last Post: TysonL
  Where to add own python modules in WinPython? HinterhaeltigesSchlaengelchen 1 2,243 Jan-21-2021, 07:45 PM
Last Post: snippsat
  Including modules in Python using sys.path.append JoeDainton123 1 2,851 Aug-24-2020, 04:51 AM
Last Post: millpond
  how to get PID's of linux commands executed through python modules? Manikandan_PS 4 2,985 Mar-12-2020, 07:16 AM
Last Post: Manikandan_PS
  python modules davy_yg 1 1,955 Sep-25-2019, 03:14 AM
Last Post: metulburr
  Importing Custom Modules in Python 3 Flexico 1 2,545 Aug-24-2019, 08:11 PM
Last Post: snippsat
  Trouble importing modules on a new python version snackman_barry 2 2,522 Jul-12-2019, 11:15 AM
Last Post: snackman_barry
  Import Python Modules zowhair 4 2,770 Jun-27-2019, 06:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020