Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在copytree()完成之前,print()不会打印_Python_Python 3.x - Fatal编程技术网

Python 在copytree()完成之前,print()不会打印

Python 在copytree()完成之前,print()不会打印,python,python-3.x,Python,Python 3.x,在Python脚本中,我复制了一个目录树,并希望打印一些如下文本 print("Copying... ", end="") shutil.copytree(src, dest) print("DONE") 但是,文本复制…直到脚本完成后才会打印。除非手动刷新缓冲区,否则输出流直到行尾才会写入终端 print("Copying... ", end="") sys.stdout.flush() shutil.copytree(src, dest) print("DONE") 这是与其他语言(C和

在Python脚本中,我复制了一个目录树,并希望打印一些如下文本

print("Copying... ", end="")
shutil.copytree(src, dest)
print("DONE")

但是,文本
复制…
直到脚本完成后才会打印。

除非手动刷新缓冲区,否则输出流直到行尾才会写入终端

print("Copying... ", end="")
sys.stdout.flush()
shutil.copytree(src, dest)
print("DONE")

这是与其他语言(C和C++)中所发现的完全相同的行为,同样的原因。

< P>使用“代码> -U/COD>选项启动Python解释器,以便使用无缓冲输出。现在您的打印消息将立即出现

您需要刷新标准输出,但无需将
sys.STDOUT
带入图片<代码>打印可以为您做到这一点:

print("Copying... ", end="", flush=True)
shutil.copytree(src, dest)
print("DONE")