简体   繁体   English

使用 shutil.move 重命名文件并将其移动到文件夹

[英]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.我有上面的 function 将文件从源文件夹移动到目标文件夹。 This function is working but the file is moving without the file extension and the file became unsupported.此 function 正在运行,但文件在没有文件扩展名的情况下移动,文件变得不受支持。

Can anyone please advise me on this issue.谁能就这个问题给我建议。

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.您需要在 src 文件中添加扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM