OS independent files

C

crystalattice

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)?
 
L

Larry Bates

crystalattice said:
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
 
S

Simon Forman

crystalattice said:
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
 
T

Tim Chase

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

Seems to work well for me.

-tkc
 
J

Jarek Zgoda

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:\.
 
C

crystalattice

Simon said:
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.
 
J

Jarek Zgoda

Tim Chase napisa³(a):
Well, there in os.path you'll find expanduser() so you can do things like


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).
 
G

Gerhard Fiedler

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
 
D

Dennis Lee Bieber

crystalattice napisa?(a):


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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
R

Roger Upole

Dennis Lee Bieber said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top