zoukankan      html  css  js  c++  java
  • python 复制/移动文件

    用python实现将某代码文件复制/移动到指定路径下。
    场景例如:mv ./xxx/git/project1/test.sh ./xxx/tmp/tmp/1/test.sh (相对路径./xxx/tmp/tmp/1/不一定存在)

    # -*- coding: utf-8 -*-
    #!/usr/bin/python
    #test_copyfile.py
    
    import os,shutil
    
    def mymovefile(srcfile,dstfile):
        if not os.path.isfile(srcfile):
            print "%s not exist!"%(srcfile)
        else:
            fpath,fname=os.path.split(dstfile)    #分离文件名和路径
            if not os.path.exists(fpath):
                os.makedirs(fpath)                #创建路径
            shutil.move(srcfile,dstfile)          #移动文件
            print "move %s -> %s"%( srcfile,dstfile)
    
    def mycopyfile(srcfile,dstfile):
        if not os.path.isfile(srcfile):
            print "%s not exist!"%(srcfile)
        else:
            fpath,fname=os.path.split(dstfile)    #分离文件名和路径
            if not os.path.exists(fpath):
                os.makedirs(fpath)                #创建路径
            shutil.copyfile(srcfile,dstfile)      #复制文件
            print "copy %s -> %s"%( srcfile,dstfile)
    
    srcfile='/Users/xxx/git/project1/test.sh'
    dstfile='/Users/xxx/tmp/tmp/1/test.sh'
    
    mymovefile(srcfile,dstfile)
    
  • 相关阅读:
    PostMan使用教程(1)
    测试工作量的评估方法
    Jmeter之Bean shell使用(二)
    centos7 编译安装redis
    Centos7 安装mariadb
    Centos7 安装使用virtualenvwrapper
    Centos7安装python3.6
    linux基础命令2
    linux基础命令
    linux目录结构
  • 原文地址:https://www.cnblogs.com/iois/p/7258686.html
Copyright © 2011-2022 走看看