Getting the Appdata Directory with Python and PEP?

E

Eamonn Rea

I've heard that there is a library that allows you to get the appdata directory for a given OS, but I'd like to do it myself, as a learning experience..

Is there a built in way to get a users Appdata Directory? For example on OSX it's in '~/Library//Application Support/'. I can get the OS just fine (sys.platform and then storing it in my own way; example: darwin = OS X, just for my own readability), and I can get the home directory just fine (expanduser), but I have no idea how to get the appdata directory.

One way I could think of doing it would be to just detect the os and join the string on like so (completely untested, but an idea);

if os == 'OS X':
appdata_dir = os.path.join(home_dir, '/Application Support/')

But then that arises the problem of cross platform compatibility.

So is here a good, cross platform solution to this problem?

Also, what is PEP, PEP8, etc? Is it like the Python programming layout conventions? Is there more to it than that?

Thanks!
 
A

Andrew Berg

I've heard that there is a library that allows you to get the appdata directory for a given OS, but I'd like to do it myself, as a learning experience.

Is there a built in way to get a users Appdata Directory? For example on OS X it's in '~/Library//Application Support/'. I can get the OS just fine (sys.platform and then storing it in my own way; example: darwin = OS X, just for my own readability), and I can get the home directory just fine (expanduser), but I have no idea how to get the appdata directory.

One way I could think of doing it would be to just detect the os and join the string on like so (completely untested, but an idea);

if os == 'OS X':
appdata_dir = os.path.join(home_dir, '/Application Support/')

But then that arises the problem of cross platform compatibility.

So is here a good, cross platform solution to this problem?
I don't know about OS X, but on Windows Vista and later, there is os.environ['APPDATA']. I don't explicitly check for OS; instead, I see if
APPDATA exists as an environment variable:
try:
user_data_dir = os.path.join(os.environ['APPDATA'], 'NoiseBot')
except KeyError:
user_data_dir = os.path.join(os.environ['HOME'], '.NoiseBot')

I didn't even know that such a thing existed on OS X.
 
E

Eamonn Rea

Thanks for the help on PEP, but I can't find a way to get the application support (appdata on Windows, no idea on Linux). If I do:

print os.environ['HOME']

I get: '/Users/eamonn', as that is my home directory. But when I do:

print os.environ['APPDATA']

I get an error. But when I do:

print os.getenv('APPDATA')

I get: None.

Apparently os.getenv() works on Windows, but I can't see a way to get this on Mac. If I could get the names of this for different OS's I could just check the OS and run the appropriate code.

Any help?
 
D

Dennis Lee Bieber

Thanks for the help on PEP, but I can't find a way to get the application support (appdata on Windows, no idea on Linux). If I do:

print os.environ['HOME']

I get: '/Users/eamonn', as that is my home directory. But when I do:

print os.environ['APPDATA']

I get an error. But when I do:

print os.getenv('APPDATA')

I get: None.

Apparently os.getenv() works on Windows, but I can't see a way to get this on Mac. If I could get the names of this for different OS's I could just check the OS and run the appropriate code.

APPDATA is a Windows conceit...

My impression is that UNIX/Linux relies upon dot-directories
(file/directory names beginning with a . being automatically hidden).

~/.myAppData

and ~ expanding to the login home directory...



Note that my Windows system doesn't /have/ a HOME variable


C:\Users\Wulfraed\Documents>set appdata
APPDATA=C:\Users\Wulfraed\AppData\Roaming

C:\Users\Wulfraed\Documents>set home
HOMEDRIVE=C:
HOMEPATH=\Users\Wulfraed

C:\Users\Wulfraed\Documents>set userprofile
USERPROFILE=C:\Users\Wulfraed

C:\Users\Wulfraed\Documents>

The closest is USERPROFILE (which is initially %HOMEDRIVE%%HOMEPATH%)

C:\Users\Wulfraed\Documents>echo %userprofile%
C:\Users\Wulfraed

C:\Users\Wulfraed\Documents>echo %homedrive%%homepath%
C:\Users\Wulfraed


PowerShell, OTOH

PS C:\Users\Wulfraed\Documents> Get-Variable home

Name Value
---- -----
HOME C:\Users\Wulfraed
 
I

Irmen de Jong

Thanks for the help on PEP, but I can't find a way to get the application support (appdata on Windows, no idea on Linux). If I do:

print os.environ['HOME']

I get: '/Users/eamonn', as that is my home directory. But when I do:

print os.environ['APPDATA']

I get an error. But when I do:

print os.getenv('APPDATA')

I get: None.

Apparently os.getenv() works on Windows, but I can't see a way to get this on Mac. If I could get the names of this for different OS's I could just check the OS and run the appropriate code.

Any help?


Maybe this module is of some use to you:
https://pypi.python.org/pypi/appdirs

It provides a unified Python API to the various OS specific 'user' directory locations.

Irmen
 
E

Eamonn Rea

Thanks for the help on PEP, but I can't find a way to get the application support (appdata on Windows, no idea on Linux). If I do:
print os.environ['HOME']
I get: '/Users/eamonn', as that is my home directory. But when I do:
print os.environ['APPDATA']
I get an error. But when I do:
print os.getenv('APPDATA')
I get: None.
Apparently os.getenv() works on Windows, but I can't see a way to get this on Mac. If I could get the names of this for different OS's I could just check the OS and run the appropriate code.
Any help?





Maybe this module is of some use to you:

https://pypi.python.org/pypi/appdirs



It provides a unified Python API to the various OS specific 'user' directory locations.



Irmen

I saw this, but I wanted to do it myself as I stated in the OP :)
 
C

Chris Angelico

I saw this, but I wanted to do it myself as I stated in the OP :)

You could read the module's source code - that's one of the coolest
benefits of open source, you can learn from someone else's
implementation.

Your posts, your replies particularly, are showing the
increasingly-annoying fingerprints of Google Groups. Look at how they
appear to us:

https://mail.python.org/pipermail/python-list/2013-November/661217.html

Note the double-spaced quoted text, and the unwrapped lines. Please do
not use Google Groups, as it's nearly impossible to use without
creating obnoxious posts.

ChrisA
 
M

Mark Lawrence

Thanks for the help on PEP, but I can't find a way to get the application support (appdata on Windows, no idea on Linux). If I do:
print os.environ['HOME']
I get: '/Users/eamonn', as that is my home directory. But when I do:
print os.environ['APPDATA']
I get an error. But when I do:
print os.getenv('APPDATA')
I get: None.
Apparently os.getenv() works on Windows, but I can't see a way to get this on Mac. If I could get the names of this for different OS's I could just check the OS and run the appropriate code.
Any help?





Maybe this module is of some use to you:

https://pypi.python.org/pypi/appdirs



It provides a unified Python API to the various OS specific 'user' directory locations.



Irmen

I saw this, but I wanted to do it myself as I stated in the OP :)

Do you realise that stock markets wordwide have plumetted again today
because of the massive surplus of newlines from this year's harvests?
The only way to get the markets back up, and with it my pension funds,
is to invest very heavily in a tool that somehow prevents this surplus.
Would you be kind enough to get one?
 
E

Eamonn Rea

Oh, sorry, I'm new to how Google Groups works. I wonder why it lays it out like that. Can it not just show quotes like the way that PHPbb does?

I never thought of reading the source code, thanks! :)

Oh, and the last message is just spam :p
 
C

Chris Angelico

Oh, sorry, I'm new to how Google Groups works. I wonder why it lays it out like that. Can it not just show quotes like the way that PHPbb does?

Google Groups is just a newsgroup client (and one of the worst in the
world, imho). Ultimately, what you're reading and writing is netnews
and a mailing list (the two are automatically linked, anything sent to
either gets copied to the other). It's fundamentally plain text, not
HTML and not PHPBB.

I recommend signing up for the mailing list, rather than using GG.

https://mail.python.org/mailman/listinfo/python-list

There are ways to use GG without it being quite as obnoxious as it
usually is, but it's a lot more work than just using a better
interface.

ChrisA
 
D

Dennis Lee Bieber

Oh, sorry, I'm new to how Google Groups works. I wonder why it lays it out like that. Can it not just show quotes like the way that PHPbb does?

Google Groups works well as long as the messages stay on GG...

It's what it does when translating between GG's HTML and the rest of
the world using NNTP/SMTP standards that causes the problems.

SMTP/NNTP standards recommend lines of <80 characters (Hollerith punch
card/old teletype/terminal physical limit). MIME Quoted-Printable permits
virtual long lines by 1) message header identifying QP format, 2) using an
=<newline> to break the long line into shorter segments. Clients would
remove the =<newline> and unwrap the longer line.

GG /appears/ to be replacing new-lines with HTTP <P> markers for
received messages. The problem is that on output back to the net, it seems
to then replace the <P> with TWO new-lines (ie; puts in a blank line
between every original line). And since it treats everything one enters as
an HTML <P>, it is sending paragraphs as one line of text.

PHPbb would have to go through hoops too, to transfer messages from its
internal format (HTML) to non-HTML NNTP/SMTP format.

GG's change (sometime last year as I recall) simplified the web-based
handling of its messages -- at the cost of trashing classical Usenet and
Mailing lists that it cross-links.
 
K

Kevin Walzer

I saw this, but I wanted to do it myself as I stated in the OP:)

This module appears to simply use hard-coded paths on Unix/Linux and OS
X--not much to learn there, except which paths to code.

--Kevin
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top