zoukankan      html  css  js  c++  java
  • python shutil 模块 的剪切文件函数 shutil.movemove(src, dst),换用 os.rename(sourceFile, targetFile)

    Google 一搜python 剪切文件,出来shutil 这模块,网上很多人也跟疯说shutil.move(src, dst)就是用来剪切文件的,结果一试,剪切毛线,文件都复制到另一个文件夹了,源文件还在,因为我的源文件正在被另一个程序使用,所以shutil.move(src, dst)把源文件复制到别的地方后没法再对源文件进行删除,这冒牌货却仍保留着复制后的文件。美其名曰移动文件。。。。网上也有人给出了shutil.move(src, dst)的源码,先来看下它的源码吧。。。

    def move(src, dst):
        real_dst = dst
        if os.path.isdir(dst):
            real_dst = os.path.join(dst, _basename(src))
            if os.path.exists(real_dst):
                raise Error, "Destination path '%s' already exists" % real_dst
        try:
            os.rename(src, real_dst)
        except OSError:
            if os.path.isdir(src):
                if destinsrc(src, dst):
                    raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
                copytree(src, real_dst, symlinks=True)
                rmtree(src)
            else:
                copy2(src, real_dst)
                os.unlink(src)

    很明显,函数没有对当前正在被别的程序使用的文件进行判断,简单的先复制再删除,删除不了的也就不管了。分析了此源码的人到最后却还在说此功能类似于windows的ctrl+x->ctrl+v操作。。你是傻吗。。。

    本来想自己写个程序判断的,但是懒得写了,最后发现用os.rename(sourceFile, targetFile)可以完美解决。。。就用它了。。。

  • 相关阅读:
    浅尝辄止——在C++中调用C#的回调函数——COM方式
    代码管理——如何连接Git Server,下载代码
    浅尝辄止——使用ActiveX装载WPF控件
    软件调试——CPU异常列表
    软件调试——IA-32 保护模式下寄存器一览
    Delphi面向对象编程
    看雪2017CTF第二题解法
    串操作指令
    MASM 重复汇编
    MASM 宏结构
  • 原文地址:https://www.cnblogs.com/homeways/p/5943854.html
Copyright © 2011-2022 走看看