0
点赞
收藏
分享

微信扫一扫

python 移动/剪切文件

在这里插入代码片
"""
Description:
  This is scripts is used to move file from a dir to the other dir
Input:
 oripath:原始文件路径
 tardir: 目标文件夹
Output:
 status: 1成功 0不成功
Example:
          movefile(E:\demo\test.txt , D:\result)
"""
def movefile(oripath,tardir):
    filename = os.path.basename(oripath)
    tarpath = os.path.join(tardir, filename)
    #判断原始文件路劲是否存在
    if not os.path.exists(oripath):
        print('the dir is not exist:%s' % oripath)
        status = 0
    else:
     #判断目标文件夹是否存在
        if os.path.exists(tardir):
        #判断目标文件夹里原始文件是否存在,存在则删除
            if os.path.exists(tarpath):
                os.remove(tarpath)
        else:
         #目标文件夹不存在则创建目标文件夹
            os.makedirs(tardir)
          #移动文件
        shutil.move(oripath, tardir)

        status = 1

    return status```

举报

相关推荐

0 条评论