Should I close a file before moving it?

前端 未结 3 1721
小鲜肉
小鲜肉 2021-01-20 18:23

I have code like this:

with open(\'foo.txt\') as file:
    ...do something with file...
    ...move foo.txt to another place while it\'s still open...
         


        
3条回答
  •  时光说笑
    2021-01-20 19:04

    Best practice with file is close and move, because if you will not close then it might be create problem in some OS, like Windows.

    To make your code more portable, close file before move.

提交回复
热议问题