擅长:python、mysql、java
<p><code>/dev/null</code>是一个特殊的设备文件(UNIX中的所有文件都是一个文件!)它像黑洞一样吞没了所有写在上面的东西。<code>dup</code>复制文件描述符。如果您习惯于Windows,UNIX中的文件描述符是一个特殊的整数,它表示一个打开的文件,它类似于Windows文件句柄。在</p>
<p>程序正在打开<code>/dev/null</code>进行写入(仅限),获取其文件描述符的副本,关闭打开的文件(因为对于UNIX来说,有一个文件描述符就足够了,您不需要保持资源打开),然后将打开的文件分配给<code>sys.stdout</code>。在</p>
<p>请记住,<code>sys</code>是Python模块,它代表各种特定于系统的资源,例如文件系统。因此,在UNIX中<code>sys.stdout</code>将表示<code>/dev/stdout</code>,即系统<code>STDOUT</code>流。在</p>
<p>总之,这段代码欺骗了Python,让它认为<code>/dev/null/</code>是{<cd8>},所以现在每当你的程序用<code>print</code>语句(Python3中的函数)写入<code>/dev/null/</code>时,它实际上是在写<code>/dev/null</code>,你永远不会在控制台上看到结果文本。在</p>