Search code examples
pythonshutilpython-watchdog

Rename and move the file to folder using shutil.move


def safe_copy(self,src,out_dir):
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    name = os.path.basename(src) 
    shutil.move(src,os.path.join(out_dir,'{}'.format(append_timestamp(name))))


safe_copy("\\\\server\\drive\\folder\\filename","\\\\server\\drive\\folder2")

I have the above function to move the file from source folder to destination folder. This function is working but the file is moving without the file extension and the file became unsupported.

Can anyone please advise me on this issue.


Solution

  • def safe_copy(src,out_dir):
       if not os.path.exists(out_dir):
         os.makedirs(out_dir)
       name = os.path.basename(src)
       shutil.move(src,os.path.join(out_dir,'{}'.format(append_timestamp(name))  + "." + src.split(".")[-1]))
    

    If you are using this move method. You need to add the extension in src file.