Using %systemdrive% in python
<p>I would like to open and work with a file with Python however I would like to use Windows <code>%systemdrive%</code> when referring to a file instead of full path. 
This piece of code works:</p>

<pre><code>if not os.path.isfile('C:\\Work\\test\sample.txt'):
</code></pre>

<p>This does not:</p>

<pre><code>if not os.path.isfile('%systemdrive%\\Work\\test\\sample.txt'):
</code></pre>

<p>Any idea? Thank you in advance!</p>

<p>There is a dedicated function for solving this problem named <a href="https://docs.python.org/2/library/os.path.html#os.path.expandvars" rel="nofollow"><code>os.path.expandvars</code></a>.</p>

<blockquote>
  <p>Return the argument with environment variables expanded. Substrings of
  the form <code>$name</code> or <code>${name}</code> are replaced by the value of environment
  variable name. Malformed variable names and references to non-existing
  variables are left unchanged.</p>
  
  <p>On Windows, <code>%name%</code> expansions are supported in addition to <code>$name</code> and
  <code>${name}</code>.</p>
</blockquote>

<pre><code>if not os.path.isfile(os.path.expandvars('%systemdrive%\\Work\\test\\sample.txt')):
    pass  # do something
</code></pre>

python windows