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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gonzalo Gonza
    New Member
    • Nov 2010
    • 16

    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:
    Code:
    file = open("C:\\Windows\\system32\\asdfasdf.txt","w")
    Because many pc's don't have their %SYSTEMDRIVE% in C:

    So, which would be the correct form?
    Thank you
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You can determine the value of SYSTEMDRIVE from os.environ['SYSTEMDRIVE'].

    Comment

    • Gonzalo Gonza
      New Member
      • Nov 2010
      • 16

      #3
      And how it would be?
      Code:
      import os
      file = open("os.environ['SYSTEMDRIVE']\\Windows\\system32\\asdfasdf.txt","w")

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I would use:
        os.path.join(os .environ['SYSTEMDRIVE'], "Windows/system32/asdfasdf.txt")

        Comment

        Working...