Which one is good practice about python formatted string?
<p>Suppose I have a file on <code>/home/ashraful/test.txt</code>. Simply I just want to open the file. 
Now my question is:</p>

<p>which one is good practice?</p>

<p><strong>Solution 1:</strong> </p>

<pre><code>dir = "/home/ashraful/"
fp = open("{0}{1}".format(dir, 'test.txt'), 'r')
</code></pre>

<p><strong>Solution 2:</strong> </p>

<pre><code>dir = "/home/ashraful/"
fp = open(dir + 'test.txt', 'r')
</code></pre>

<p>The both way I can open file. </p>

<p>Thanks :) </p>

<p>instead of concatenating string use <code>os.path.join</code> <code>os.path.expanduser</code> to generate the path and open the file. (assuming you are trying to open a file in your home directory)</p>

<pre><code>with open(os.path.join(os.path.expanduser('~'), 'test.txt')) as fp:
    # do your stuff with file
</code></pre>

python file