Print directory where file is saved with Python
<p>how could do print the path directory of my file saved with python.
For ad example, i start with idle3 (Fedora 24), i save the IDLE code on: /home/jetson/Desktop/Python/Programs and in the code is written:</p>

<pre><code>f = open("Hello.txt","w")
f.write("This is a test","\n")
f.close()
</code></pre>

<p>My question is: after f.close i would print the directory grabbed with </p>

<pre><code>import sys.os 

print("The file is saved in ",direcotrysaved)
</code></pre>

<p>It's possible?</p>

<p>Convert the relative path to absolute path:</p>

<pre><code>path = os.path.abspath(filename)
</code></pre>

<p>Then take just the directory from the absolute path:</p>

<pre><code>directory = os.path.dirname(path)
</code></pre>

<p>That will work regardless of what <code>filename</code> is. It could by just a filename, a relative path or absolute path.</p>

<p>See <a href="https://docs.python.org/3/library/os.path.html" rel="nofollow">os.path documentation</a> for other useful functions.</p>

python python-3.x