How to move a file?

后端 未结 9 1858
夕颜
夕颜 2020-11-22 01:07

I looked into the Python os interface, but was unable to locate a method to move a file. How would I do the equivalent of $ mv ... in Python?

&g         


        
9条回答
  •  失恋的感觉
    2020-11-22 01:22

    After Python 3.4, you can also use pathlib's class Path to move file.

    from pathlib import Path
    
    Path("path/to/current/file.foo").rename("path/to/new/destination/for/file.foo")
    

    https://docs.python.org/3.4/library/pathlib.html#pathlib.Path.rename

提交回复
热议问题