0

I developed a big python project, that does specific tasks. Later on I decided to add a graphical user interface for it.

├── GUI
|
└── myScript

Let's say the GUI has a "Start" button. When clicking on it, we start the business logic that is located in "myScript".

Currently I implemented the interaction this way:

Inside the GUI:

os.chdir("../myScript")
main.start() #calling the business function from myScript
os.chdir("../GUI")

I needed to add os.chdir because I had to deal with a lot of file reading and writing inside myScript. (I used absolute paths to find files)

I don't feel like this is the best way to link the presentation layer to the business layer. Is there a better way to do this in Python?

Gaston
  • 185
  • 8
  • 1
    *"I needed to add `os.chdir` because I had to deal with a lot of file reading and writing inside `myScript`."* -- Please elaborate on that. What kind of files are you talking about? Maybe temp files? Why do they live in the application folder instead of a dedicated folder like `/tmp` or `~/.myScript`? – wjandrea Jul 27 '22 at 18:55
  • The only thing that's wrong with that is that the business logic ASSUMES a given working directory. If it needs files that are in its same directory, it should use `os.dirname(__file__)` to figure out where it is located, and use that as its starting path. – Tim Roberts Jul 27 '22 at 18:56
  • What kind of GUI are you using tkinter or pyQt any way why don't you use open dir dialog to get the parent dir path then work with sub dirs – Stefano Jul 27 '22 at 19:10
  • @wjandrea it's just normal files that I need to read and write in inside the application. – Gaston Jul 27 '22 at 19:14
  • @Stefano pyqt, I'll have a look into that, thanks – Gaston Jul 27 '22 at 19:15
  • @TimRoberts, I'll have a look into that, thanks – Gaston Jul 27 '22 at 19:16
  • @Gaston What do you mean by "normal files"? Why do you need to write to files inside the application folder? – wjandrea Jul 27 '22 at 19:19
  • Sorry not normal files. To be more precise, the application folder contains temporary files, like you said that will be modified then moved to another destination. – Gaston Jul 27 '22 at 19:34

0 Answers0