%SystemDrive%

R

rtilley

Is there a proper way to get this variable from Windows? I know it's in
the registry, but I'd rather not go there. I could not find a CSIDL
shell constant for it either. I've been doing this:

os.chdir('/')
sys_drive = os.getcwd()
print sys_drive
C:\

This seems too much of a hack and maybe not 100% right all of the time.
How could it be done better?
 
C

Carsten Haese

Another hack:
drive = os.popen("echo %SYSTEMDRIVE%").readline().strip()

Is there a reason why os.environ['SYSTEMDRIVE'] shouldn't work?

Hope this helps,

Carsten.
 
R

rtilley

Carsten said:
Is there a reason why os.environ['SYSTEMDRIVE'] shouldn't work?

Hope this helps


I didn't know it was in os! It returns C: instead of C:\ like my method.
Other than that, it seems to do the trick.

Thank you!
Brad
 
A

Atanas Banov

using
os.chdir('/')
os.getcwd()
is plain wrong in Windows.

what it does is change the current directory to root of the CURRENT
DRIVE (i.e. the drive of the directory where script was started from),
not the system drive. for example, if current directory was
c:\myscripts and system drive is d:, this will return c:\ and not d:\.
hence chdir doesnt do any good, you can do os.getcwd()[:3] with the
same success.

os.environ['SYSTEMDRIVE'] is the right way to go.
btw it correctly returns just drive letter and colon, a drive name
doesnt include [back]slash
 
R

rtilley

Atanas said:
using
os.chdir('/')
os.getcwd()
is plain wrong in Windows.

what it does is change the current directory to root of the CURRENT
DRIVE (i.e. the drive of the directory where script was started from),
not the system drive. for example, if current directory was
c:\myscripts and system drive is d:, this will return c:\ and not d:\.
hence chdir doesnt do any good

Thanks for the explanation. I'll use the environ feature of os... I just
didn't realize it existed :)
 
B

Bryan Olson

rtilley said:
Carsten said:
Is there a reason why os.environ['SYSTEMDRIVE'] shouldn't work?

I didn't know it was in os! It returns C: instead of C:\ like my method.
Other than that, it seems to do the trick.

To get it with the \, you might use:

os.path.abspath(os.environ['SYSTEMDRIVE'])
 
A

Atanas Banov

Bryan said:
To get it with the \, you might use:

os.path.abspath(os.environ['SYSTEMDRIVE'])

wrong!
the result is incorrect if the current directory is different from the
root.
os.chdir("c:\\winxp")
os.path.abspath(os.environ['SYSTEMDRIVE'])
'c:\\winxp'

if you really want it with backslash at the end, why not just add it

os.environ['SYSTEMDRIVE'] + '\\'

is it too simple?!
 
B

Bryan Olson

Atanas said:
Bryan said:
To get it with the \, you might use:

os.path.abspath(os.environ['SYSTEMDRIVE'])


wrong!
the result is incorrect if the current directory is different from the
root.

Oops, sorry. I should know better than to code from what I
think I vaguely remember.
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top