By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

How to use Environment Variables to set location of file in Python?

P: 16
Hello there,
I want to create a txt in system32, but I want to do that using an environment variable.
I want to use an environment variable instead of this:
Expand|Select|Wrap|Line Numbers
  1. file = open("C:\\Windows\\system32\\asdfasdf.txt","w")
  2.  
Because many pc's don't have their %SYSTEMDRIVE% in C:

So, which would be the correct form?
Thank you
Dec 6 '10 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You can determine the value of SYSTEMDRIVE from os.environ['SYSTEMDRIVE'].
Dec 6 '10 #2

P: 16
And how it would be?
Expand|Select|Wrap|Line Numbers
  1. import os
  2. file = open("os.environ['SYSTEMDRIVE']\\Windows\\system32\\asdfasdf.txt","w")
Dec 6 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I would use:
os.path.join(os.environ['SYSTEMDRIVE'], "Windows/system32/asdfasdf.txt")
Dec 7 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.