1

I've been learning Python and decided to make a note-taking utility that runs in bash. I've worked out the basic 'guts' and I want to add a feature that allows a new user to configure their notes (for instance, set the directory where new note files are stored).

I realize this means running a 'install/config' function that is only called the first time the user runs the script (or until they configure it). I don't know what this concept is called, and after some research, cannot find anything about it w/Python.

I'm using argparse. You call the python script from the shell and can optionally use it with arguments. If it would help to see my code, please let me know and I'll format it (it's long and needs to be edited a bit if I want to post). Thanks.

tl;dr How do you run a function only once in Python (either first time code is executed, or until the function's purpose - in this case, setting a file path - is fulfilled)?

struensee
  • 566
  • 8
  • 17
  • 2
    Why not simply [check if the settings file exists](https://stackoverflow.com/questions/82831/how-do-i-check-whether-a-file-exists-without-exceptions)? If you store the user's settings in, e.g., the file `~/.my_app_settings`, the mere existence of this file indicates that the script has been previously run. – Brian61354270 Mar 14 '20 at 00:01
  • Huh. Now I feel pretty dumb, haha. That's a good, clean solution. I'm curious how most Python apps/scripts would handle this. Is it common to do it the way you suggested? – struensee Mar 14 '20 at 00:30
  • 1
    @goldmund. There are any number of ways of doing it, but they all boil down to basically that exact thing. If you want a cross-platform solution, it's way to go. – Mad Physicist Mar 14 '20 at 02:57

0 Answers0