<p>新的打开文件模式“x”在python 3中做什么?</p>
<p>这是<a href="https://docs.python.org/3/library/functions.html#open">python 3</a>的文件:</p>
<blockquote>
<p>'r': open for reading (default)</p>
<p>'w': open for writing, truncating the file first</p>
<p>'x': open for exclusive creation, failing if the file already exists</p>
<p>'a': open for writing, <a href="https://www.cnpython.com/list/append" class="inner-link">append</a>ing to the end of the file if it exists</p>
<p>'b': binary mode</p>
<p>'t': text mode (default)</p>
<p>'+': open a disk file for updating (reading and writing)</p>
<p>'U': universal newlines mode (deprecated)</p>
</blockquote>
<p>“独家创作”是什么意思?</p>
<p>我测试了“x”模式并找到了一些:</p>
<ul>
<li>不能与“r/w/a”一起使用</li>
<li>“x”只可写x+“可以读写</li>
<li>文件必须在<code>open</code>之前不存在</li>
<li>文件将在<code>open</code>之后创建</li>
</ul>
<p>所以“x”与“w”类似。但对于“x”,如果文件存在,则引发FileExistsError</strong>。对于“w”,它只需创建一个新文件/截断现有文件。</p>
<p>我说得对吗?这是唯一的区别吗?</p>