2

Resolution: As Thomas pointed out below, the task I have been given is near to impossible to achieve, and the data must be on the fly determined since the template structure used by ANY OS can be overridden fairly easily, and there is NEVER a way to guarantee that a pre-built template can be correctly applied. My thanks to all who replied (even if I was to dense to get it in the first place...) :-P

I have no access to a mac or windows based machine at this time and I am working on a cross-platform project(Win/Lin/Mac).

On linux the return from a call to

os.path.expanduser('~')

returns /home/<user>

Now in any operating system the $home directory structure contains a final point which is common to all paths, (On my linux install the home paths are /home/<user> by default, making /home the last common point).

So My question (which I cant simply test at the moment) is this: On windows and mac, barring some unusual reconfiguration (software product is for average joe blow home users and teenagers) is the final part of the returned path always the first non-common path part?

(This question could be made more clear but I am stumbling for how to explain this. This is rewrite three.)

EDIT: After reading an article linked by phihag in the comments below, I think he is asking for better clarification of my intended use (The impossible specification thing). What I am trying to do is determine the part of the return from os.path.expanduser('~') which I can convert to be the first part of new relative path definition from which I could then create further directory structures which are application specific for each user. The equivalent of /home/<user>/.config/<MyAppHere> or \Documents and Settings\<user>\Application Data\Roaming\<MyAppHere>. My final result will use (if this question can be determined True for my intentions) the following:

# Assumes linux system and home path of /home/<user
head, tail = os.path.split(os.path.expanduser('~'))
userConfigPath = os.path.join(tail,'.config',MYAPPNAME)

# userConfigPath then gets written to a config file which is used by the program
# to determine where (within a greater structure) to look for a user based
# configuration which expands or overrides system configuration options.

EDIT: After exchanging info with Thomas Orozco (below): At this stage of the software execution, it doesn't know where to look for custom user configurations. What is going to be read from the config file is not an absolute address, but a template of how to build an absolute address for a config file.

The software in question is related to video games and has an intended user base of teenagers or adults working on a standard home computer. Most of these users do not utilize special configuration options like customized home user directory locations. This portion of the code works with the so called norms for any OS, and disregards the possibilities of customizations to the OS in question. While is bad practice programmaticly, it is what I am currently stuck with.

4

1 に答える 1

4

This is not even true on Linux. Just have a look at /etc/adduser.conf:

  • GROUPHOMES=yes would let adduser create homes such as: /home/groupname/user.
  • LETTERHOMES=yes would create ones looking like: /home/u/user.

And that's just the default. You can put you home anywhere you like.


Regarding what you're trying to achieve, why isn't the following appropriate?

from os.path import join, expanduser
join(expanduser('~'), '.config') # You could have something more funky if you want to. 

Then, just compute this path on the fly when the application is run, without hardcoding it into your app.

于 2012-12-31T15:10:19.343 に答える