Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > OS independent files

Reply
Thread Tools

OS independent files

 
 
crystalattice
Guest
Posts: n/a
 
      08-03-2006
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)?

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      08-03-2006
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
 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      08-03-2006
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

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      08-03-2006
> 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



 
Reply With Quote
 
Jarek Zgoda
Guest
Posts: n/a
 
      08-03-2006
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/
 
Reply With Quote
 
crystalattice
Guest
Posts: n/a
 
      08-03-2006

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.

 
Reply With Quote
 
Jarek Zgoda
Guest
Posts: n/a
 
      08-03-2006
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/
 
Reply With Quote
 
Gerhard Fiedler
Guest
Posts: n/a
 
      08-03-2006
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

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      08-04-2006
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\Curre ntVersion\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: web-)
HTTP://www.bestiaria.com/
 
Reply With Quote
 
Roger Upole
Guest
Posts: n/a
 
      08-04-2006

"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\Curre ntVersion\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/ar.../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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Independent processes john VHDL 3 01-10-2006 04:40 PM
Independent processes john VHDL 0 01-09-2006 07:43 PM
Route 4 T1 lines with independent VLANs mcarroll76@gmail.com Cisco 6 04-03-2005 10:01 PM
mcsd .net independent consultants Session MCSD 2 06-02-2004 05:21 PM
Running exe as an independent process Hagay Lupesko ASP .Net 1 10-26-2003 07:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57