1

I'm a self-taught, amateur, purely recreational programmer. I don't understand all the fancy programming lingo, and I certainly don't have any good resources, apart from this website, where I can go for help. (i.e., Please dumb it down for me!) I would imagine my question here is somewhat common, but I honestly couldn't find any answers on Google or this website, probably because I don't know the proper terminology to search for.

~~~

Having said that, I feel I have a pretty solid grasp on the basics of Python. And now, I've created an application that I'd like to share with a friend. My application accesses JPEG image files on my computer using a directory path that I've written into the code itself. However, I'd like my friend to be able to store these image files anywhere on their computer, not necessarily in the file folder that I've been using.

I assume the best way to accomplish this is to allow my friend to choose the directory path for themselves and then to write their chosen directory path to a file at a predetermined location on their computer. My application would then have that file's location prewritten into its code. This way, it would be trivially easy to open the file at the predetermined location, and then that file would point my application to my friend's chosen directory path.

1.) Are any of my intuitions here misguided? Are there better ways of doing this?

2.) If you think my general approach is a reasonable one, then is there a good/common place on the computer where applications typically store their directory paths upon installation?

Any advice - or any recommended resources - would be very much appreciated! Thanks!

Mike S.
  • 13
  • 2
  • That is absolutely correct. The bad news is that it's OS-specific, and you need to understand how all 3 work at that level in order to do it correctly. – Ignacio Vazquez-Abrams Nov 05 '14 at 23:57
  • OK, but where can I go to learn how to do that? What's the first step? Is there an article / resource somewhere that could get me started? – Mike S. Nov 06 '14 at 00:42

1 Answers1

1

Well, the standard way to do this is a lot more complicated and platform-specific:

On traditional Unix, this is pretty simple; you create a text file in some simpler format (e.g., that used by ConfigParser, named, say, ~/.myprogram.cfg, and you write a line to it that looks like image_path=/path/to/images.

On most modern Linux systems, or any other FreeDesktop/XDG-based system, you should (at least for GUI apps) instead use a special directory looked up in the environment as XDG_CONFIG_HOME, falling back to ~/.config, instead of using ~.

On Windows, the standard place to store stuff like this is the Windows Registry (e.g., by using winreg), by creating a key for your program and storing a value with name image_path and value /path/to/images there.

On Mac, the standard place to store stuff like this is in the NSUserDefaults database (e.g., by using PyObjC, which isn't part of the stdlib but does come built-in with Apple's pre-installed Python) by opening the default domain for your program and adding a value with key image_path and value… well, you probably want a Cocoa bookmark (maybe even a security-scoped one), not a path.

That probably all sounds way, way too complicated.

One option is to use a library that wraps this all up for you. If you're already using a heavy-duty framework like, say, Qt, it probably has functionality built-in to do that. Otherwise, it may take a lot of searching to find something.

A simpler alternative is to just pretend everything is like traditional Unix. That will work on Windows and Mac. It will be slightly annoying on some Windows versions that your config file will be visible in their home directory, but not a huge deal. It means you won't get some of the bonus features that Mac provides, like being able to magically follow the directory if the user moves it somewhere else on his hard drive, or remembering the settings if he reinstalls OS X and migrates his old settings, but again, usually that's fine.

In between the extremes, you can pretend everything is like Linux, using a special, and unobtrusive, location for the files on Windows and Mac just as you do there. Both platforms have APIs to look up special directories, called "application data" on Windows and "application support" on Mac. Using PyWin32 or PyObjC, respectively, these are pretty easy to look up. (For example, see this answer.) Then you just create a subdirectory there named My App on Windows, or com.mydomain.myapp on Mac, and store the file there.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thank you, thank you, thank you, thank you! You're awesome! Totally answered my question. This is exactly what I was looking for! – Mike S. Nov 06 '14 at 00:47
  • @MikeS.: Do you need any additional info, like where to find the MSDN and Apple docs on how to name your registry key/prefs domain or which functions to call, or is this enough to get started? – abarnert Nov 06 '14 at 01:16
  • @abarnert: Are you suggesting there's a particular way I'm supposed to name my registry entries in order for them to work properly? If I wanted to do this for Windows, for example, couldn't I just create a registry associated with my application and then create any keys as I see fit? Or is there a particular way I'm supposed to do it? And I think I can figure out the modules and functions on my own, but I appreciate the help :) – Mike S. Nov 06 '14 at 03:53
  • @MikeS.: The `image_path` part, you can call anything you want. I usually try to use the same names for reg-keys/prefs/config-entries across platforms, but you don't even have to do that, you can load `ImagePath` on one platform, `Image Path` on another, and `image_path` on another. – abarnert Nov 06 '14 at 21:07
  • @MikeS.: The top-level key does have a quasi-standard; I believe the current version still recommends `HKCU\Software\My Identity\My Program` (or `HKCU\Software\My Identity\My Program\1.2` if you want different versions to coexist). Here I'd try to use the same basic words, but the formatting can't really be the same (your Mac prefs domain will be something like `com.myidentity.myprogram`, but you don't want your program name on Windows to by `myprogram`). – abarnert Nov 06 '14 at 21:16