0

I am trying to figure out the best way to move one file from a specified folder into another specified folder using python. I managed to find two different ways to do that.

os.rename("path/to/current/filename.txt","path/to/new/desination/for/filename.txt")

shutil.move("path/to/current/filename.txt", "path/to/new/destination/for/file.txt")

Which method is better to use in order to move the file to the destination folder and delete any duplication files that might occur there? Thanks.

scrapy
  • 15
  • 5
  • 1
    Possible duplicate of [How to move a file in Python](http://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python) – Ledwith94 Apr 27 '17 at 16:10
  • do `help(os.rename)` and `help(shutil.move)` in python console so you can see why you better stick with `move` and not `rename` (at least if you don't take into account their names) – sepulchered Apr 27 '17 at 16:12

3 Answers3

0

Yes, It will be moved (not copied) to the new destination. os.rename() is just like mv in shell. It will rename/move the file.

you can test it will a custom script also.

import os

os.rename("src", "dst")
rrawat
  • 1,071
  • 1
  • 15
  • 29
0

os.rename moves the file from source to destination folder!

It is similar to mv shell command

If you want to copy a file, you can use shutil.copyfile() - Reference

from shutil import copyfile    
copyfile(src, dst)

Happy Coding!

Community
  • 1
  • 1
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
0

It will rename it, which means the original file will be 'moved' to the destination. If you doubt it, just try it out with a test file

Silencer310
  • 862
  • 2
  • 8
  • 25