473,378 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
3 2697
bvdet
2,851 Expert Mod 2GB
You can determine the value of SYSTEMDRIVE from os.environ['SYSTEMDRIVE'].
Dec 6 '10 #2
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
2,851 Expert Mod 2GB
I would use:
os.path.join(os.environ['SYSTEMDRIVE'], "Windows/system32/asdfasdf.txt")
Dec 7 '10 #4

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

Similar topics

3
by: Jim Potts | last post by:
What is the best way in Python version 2.x to read/get Windows (nt/2000/xp) system and user environment variables? Thanks for the help! jep
6
by: Matthew Barnes | last post by:
I'm considering submitting a patch for Python 2.4 to allow environment variable expansion in ConfigParser files. The use cases for this should be obvious. I'd like to be able to specify something...
3
by: Greg Lindstrom | last post by:
Hello- I am running python 2.3. on an HP-9000 box running Unix and have a POSIX script that sets up my production environment. I would like to run the script from inside a python routine and...
3
by: David Durkee | last post by:
Hi, I'm trying to write a script I can run from tcsh in Terminal (on Mac OS X) that will set environment variables that can be accessed by subsequent commands I execute in that session. Not...
0
by: Andy Gimblett | last post by:
Hi all, Question: is there any way to use environment variables (or something similar) in my saved SQL code, and have them expanded at runtime? I can't see a way to do this in the manuals, and...
0
by: Tristan | last post by:
Hello community: I post this because I could not find satisfactory answers in the posts generated by this nice group. I work on winXP. I have many little python applications in different folders,...
3
by: Tristan | last post by:
Hello community: I post this because I could not find satisfactory answers in the posts generated by this nice group. I work on winXP. I have many little python applications in different folders,...
5
by: Maxim Veksler | last post by:
Hello list, I'm trying to write a python script that would allow me to manipulate shell variables of the calling shell. I'm trying to write some logic that would know to add LD_LIBRARY_PATH to...
4
by: Krishna | last post by:
Environment variable set up is the most confusing part for me all the time. Please help me with the following questions: When I install python in a new system, I will go to environment variables...
3
by: smitty1e | last post by:
Just a fun exercise to unify some of the major input methods for a script into a single dictionary. Here is the output, given a gr.conf file in the same directory with the contents stated below: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.