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

Rob*_*uch 2 python python-3.x

在Python脚本中,我复制了一个目录树,并希望打印一些像这样的文本......

print("Copying... ", end="")
shutil.copytree(src, dest)
print("DONE")
Run Code Online (Sandbox Code Playgroud)

但是,Copying...在脚本完成之前不会打印文本.

Die*_*Epp 6

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

print("Copying... ", end="")
sys.stdout.flush()
shutil.copytree(src, dest)
print("DONE")
Run Code Online (Sandbox Code Playgroud)

这与您在其他语言(如C和C++)中找到的完全相同的行为也是出于同样的原因.