OS independent files

Discussion in 'Python' started by crystalattice, Aug 3, 2006.

  1. I'm sure this has been addressed before but it's difficult to search
    through several thousand postings for exactly what I need, so I
    apologize if this a redundant question.

    I've figured out how to use os.path.join to make a file or directory
    location prior to pickling something to it. But I have a question
    about it. In Windows I can make a file with this:

    os.path.join("C:", "myfiles", "myfile.dat")

    If I want to make sure the file/directory is made in a user's home
    directory (e.g. /home/users/path/to/file) but also compatible w/
    Windows, how would I rewrite this (if required)?
     
    crystalattice, Aug 3, 2006
    #1
    1. Advertising

  2. crystalattice

    Larry Bates Guest

    crystalattice wrote:
    > I'm sure this has been addressed before but it's difficult to search
    > through several thousand postings for exactly what I need, so I
    > apologize if this a redundant question.
    >
    > I've figured out how to use os.path.join to make a file or directory
    > location prior to pickling something to it. But I have a question
    > about it. In Windows I can make a file with this:
    >
    > os.path.join("C:", "myfiles", "myfile.dat")
    >
    > If I want to make sure the file/directory is made in a user's home
    > directory (e.g. /home/users/path/to/file) but also compatible w/
    > Windows, how would I rewrite this (if required)?
    >

    I don't believe there is an "automatic" way to do this, but you can
    check which OS you are running under and branch accordingly. For
    examples on how to do this take a look at /lib/os.py source code that
    comes with Python. It seems to check sys.builtin_module_names to
    determine which OS it is running on. Then you can use os.path.join()
    to build path to your "home" directory. You will probably also need
    to look at os.environ to get some info from the user's environment.
    The "trickiest" part will be to find what code is necessary to track
    down the users "home directory" on each different operating system.

    Hope this helps.

    -Larry Bates
     
    Larry Bates, Aug 3, 2006
    #2
    1. Advertising

  3. crystalattice

    Simon Forman Guest

    crystalattice wrote:
    > I'm sure this has been addressed before but it's difficult to search
    > through several thousand postings for exactly what I need, so I
    > apologize if this a redundant question.


    Google groups has a very good search.

    > I've figured out how to use os.path.join to make a file or directory
    > location prior to pickling something to it. But I have a question
    > about it. In Windows I can make a file with this:
    >
    > os.path.join("C:", "myfiles", "myfile.dat")
    >
    > If I want to make sure the file/directory is made in a user's home
    > directory (e.g. /home/users/path/to/file) but also compatible w/
    > Windows, how would I rewrite this (if required)?


    Try os.path.expanduser('~') (in
    http://docs.python.org/lib/module-os.path.html) or you could just look
    up the HOME environment variable in os.environ, but I don't know if
    windows sets this correctly in all cases.

    (os.environ is documented in
    http://docs.python.org/lib/os-procinfo.html)

    Peace,
    ~Simon
     
    Simon Forman, Aug 3, 2006
    #3
  4. crystalattice

    Tim Chase Guest

    > location prior to pickling something to it. But I have a question
    > about it. In Windows I can make a file with this:
    >
    > os.path.join("C:", "myfiles", "myfile.dat")
    >
    > If I want to make sure the file/directory is made in a user's home
    > directory (e.g. /home/users/path/to/file) but also compatible w/
    > Windows, how would I rewrite this (if required)?



    Well, there in os.path you'll find expanduser() so you can do
    things like

    >>> homedir = os.path.expanduser("~")
    >>> filename = os.path.join(homedir, "myfiles", "myfile.dat")


    Seems to work well for me.

    -tkc
     
    Tim Chase, Aug 3, 2006
    #4
  5. crystalattice

    Jarek Zgoda Guest

    crystalattice napisa³(a):

    > If I want to make sure the file/directory is made in a user's home
    > directory (e.g. /home/users/path/to/file) but also compatible w/
    > Windows, how would I rewrite this (if required)?


    On Windows, there's no notion of user's home directory, there is a
    directory for user's profile that is treated as $HOME but isn't
    (%USERPROFILE%), something that looks like $HOME, bot in fact is not
    (%HOMEDRIVE% + %HOMEPATH%) and many other mess. Microsoft suggests using
    %USERPROFILE%, but they do not specify desired behaviour, when
    %USERPROFILE% == c:\.

    --
    Jarek Zgoda
    http://jpa.berlios.de/
     
    Jarek Zgoda, Aug 3, 2006
    #5
  6. Simon Forman wrote:
    > Google groups has a very good search.


    That's what I'm using, and it still came up with 600-900 results
    depending on my search terms.

    > Try os.path.expanduser('~') (in
    > http://docs.python.org/lib/module-os.path.html) or you could just look
    > up the HOME environment variable in os.environ, but I don't know if
    > windows sets this correctly in all cases.
    >
    > (os.environ is documented in
    > http://docs.python.org/lib/os-procinfo.html)
    >
    > Peace,
    > ~Simon


    I'll try that. Thanks.
     
    crystalattice, Aug 3, 2006
    #6
  7. crystalattice

    Jarek Zgoda Guest

    Tim Chase napisa³(a):

    > Well, there in os.path you'll find expanduser() so you can do things like
    >
    >>>> homedir = os.path.expanduser("~")
    >>>> filename = os.path.join(homedir, "myfiles", "myfile.dat")

    >
    > Seems to work well for me.


    On Windows, this is consistent between releases (i.e. you'll end up
    elsewhere on W2k Pro, W2k Server, Win XP and W2003).

    --
    Jarek Zgoda
    http://jpa.berlios.de/
     
    Jarek Zgoda, Aug 3, 2006
    #7
  8. On 2006-08-03 16:21:13, crystalattice wrote:

    > I'm sure this has been addressed before but it's difficult to search
    > through several thousand postings for exactly what I need, so I
    > apologize if this a redundant question.
    >
    > I've figured out how to use os.path.join to make a file or directory
    > location prior to pickling something to it. But I have a question
    > about it. In Windows I can make a file with this:
    >
    > os.path.join("C:", "myfiles", "myfile.dat")
    >
    > If I want to make sure the file/directory is made in a user's home
    > directory (e.g. /home/users/path/to/file) but also compatible w/
    > Windows, how would I rewrite this (if required)?


    On more recent systems at least (2k, XP) you have the following environment
    variables. The values below are typical values; they are not necessarily
    like that.

    USERNAME=<user>
    HOMEDRIVE=C:
    HOMEPATH=\Documents and Settings\<user>
    USERPROFILE=C:\Documents and Settings\<user>
    ALLUSERSPROFILE=C:\Documents and Settings\All Users
    APPDATA=C:\Documents and Settings\<user>\Application Data

    APPDATA\<applicationname> is a typical place to put application data.

    Gerhard
     
    Gerhard Fiedler, Aug 3, 2006
    #8
  9. On Thu, 03 Aug 2006 21:55:21 +0200, Jarek Zgoda <>
    declaimed the following in comp.lang.python:

    > crystalattice napisa?(a):
    >
    > > If I want to make sure the file/directory is made in a user's home
    > > directory (e.g. /home/users/path/to/file) but also compatible w/
    > > Windows, how would I rewrite this (if required)?

    >
    > On Windows, there's no notion of user's home directory, there is a
    > directory for user's profile that is treated as $HOME but isn't
    > (%USERPROFILE%), something that looks like $HOME, bot in fact is not
    > (%HOMEDRIVE% + %HOMEPATH%) and many other mess. Microsoft suggests using
    > %USERPROFILE%, but they do not specify desired behaviour, when
    > %USERPROFILE% == c:\.


    There is also the registry entry

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
    Folders\Personal

    which, on my machine, contains the value

    E:\UserData\Dennis Lee Bieber\My Documents
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Aug 4, 2006
    #9
  10. crystalattice

    Roger Upole Guest

    "Dennis Lee Bieber" <> wrote:
    > On Thu, 03 Aug 2006 21:55:21 +0200, Jarek Zgoda <>
    > declaimed the following in comp.lang.python:
    >
    >> crystalattice napisa?(a):
    >>
    >> > If I want to make sure the file/directory is made in a user's home
    >> > directory (e.g. /home/users/path/to/file) but also compatible w/
    >> > Windows, how would I rewrite this (if required)?

    >>
    >> On Windows, there's no notion of user's home directory, there is a
    >> directory for user's profile that is treated as $HOME but isn't
    >> (%USERPROFILE%), something that looks like $HOME, bot in fact is not
    >> (%HOMEDRIVE% + %HOMEPATH%) and many other mess. Microsoft suggests using
    >> %USERPROFILE%, but they do not specify desired behaviour, when
    >> %USERPROFILE% == c:\.

    >
    > There is also the registry entry
    >
    > HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
    > Folders\Personal
    >
    > which, on my machine, contains the value
    >
    > E:\UserData\Dennis Lee Bieber\My Documents


    According to this:
    http://blogs.msdn.com/oldnewthing/archive/2003/11/03/55532.aspx
    the Shell Folders key shouldn't be relied upon.
    You can retrieve user directories using SHGetFolderPath
    or SHGetSpecialFolderPath, both of which are wrapped by
    win32com.shell.

    Roger
     
    Roger Upole, Aug 4, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john

    Independent processes

    john, Jan 9, 2006, in forum: VHDL
    Replies:
    0
    Views:
    488
  2. john

    Independent processes

    john, Jan 9, 2006, in forum: VHDL
    Replies:
    3
    Views:
    492
  3. Hagay Lupesko

    Running exe as an independent process

    Hagay Lupesko, Oct 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    428
    Joshua Flanagan
    Oct 26, 2003
  4. Cowboy
    Replies:
    1
    Views:
    340
    Joe Fallon
    May 27, 2004
  5. ibeetb
    Replies:
    1
    Views:
    465
    Steve C. Orr [MVP, MCSD]
    Jun 1, 2004
Loading...

Share This Page