Why a filename is given closefd parameter of open() Function must be True in Python 3.5.2?
<p>In Python 3.5.2, when I give False value to closefd parameter of open() function with a filename, I get this error below:</p>

<pre><code>Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ValueError: Cannot use closefd=False with file name
</code></pre>

<p>Results for me of my question (Why a filename is given closefd of open() Function must be True in Python 3.5.2?) and queries similiar to my question (e.g. closefd parameter in python) in Google Search isn't satisfying, the link of the results is below (You will may get different results.):</p>

<p><a href="https://www.google.com/#q=Why+a+filename+is+given+closefd+parameter+of+open()+function+must+be+True+in+Python+3.5.2%3F" rel="nofollow">https://www.google.com/#q=Why+a+filename+is+given+closefd+parameter+of+open()+function+must+be+True+in+Python+3.5.2%3F</a></p>

<p>In Python 3.5.2 documentation, there is only a description for closefd parameter of open() function (The link of open() function in Python 3.5.2 documentation is <a href="https://docs.python.org/3/library/functions.html#open" rel="nofollow">https://docs.python.org/3/library/functions.html#open</a>). The description is below:</p>

<blockquote>
  <p>If closefd is False and a file descriptor rather than a filename was given, the underlying file descriptor will be kept open when the file is closed. If a filename is given closefd must be True (the default) otherwise an error will be raised.</p>
</blockquote>

<p>I couldn't find the answer of this question. My Python version string that first outputted in my Python command line interpreter is below:</p>

<pre><code>Python 3.5.2 (default, Jul  5 2016, 12:43:10) 
[GCC 5.4.0 20160609] on linux
</code></pre>

<p>In Python, <a href="https://docs.python.org/3/library/functions.html#open" rel="nofollow">open()</a> has two purposes:</p>

<ol>
<li>To open a given file from its name, or</li>
<li>To wrap a file descriptor representing an already open file.</li>
</ol>

<p>The second purpose is useful if you have a file descriptor and you want to use portable Python functions (rather than the <a href="https://docs.python.org/2/library/os.html#file-descriptor-operations" rel="nofollow">os</a> module) to manipulate it. However, in that case, the file descriptor will be closed when the Python file object wrapper is closed, which you may not want (as you may want the file descriptor to remain valid for further uses).</p>

<p>Therefore, the <code>open()</code> function provides an argument that allows you to specify you want the file descriptor to remain open. Since this behavior only makes sense when you pass a file descriptor to <code>open()</code>, an error is raised if you try to use it in conjunction with a file name.</p>

python python-3.x