pythonfilefile-handlingpython-os

How do I move a file in Python?


How can I do the equivalent of mv in Python?

mv "path/to/current/file.foo" "path/to/new/destination/for/file.foo"

Solution

  • os.rename(), os.replace(), or shutil.move()

    All employ the same syntax:

    import os
    import shutil
    
    os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
    os.replace("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
    shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")