How to copy a file using python?

This question already has an answer here:

  • How do I copy a file in python? 14 answers

  • 要创建所有中间级目标目录,可以在复制之前使用os.makedirs()

    import os
    import shutil
    
    srcfile = 'a/long/long/path/to/file.py'
    dstroot = '/home/myhome/new_folder'
    
    
    assert not os.path.isabs(srcfile)
    dstdir =  os.path.join(dstroot, os.path.dirname(srcfile))
    
    os.makedirs(dstdir) # create all directories, raise an error if it already exists
    shutil.copy(srcfile, dstdir)
    

    take a look at shutil . shutil.copyfile(src, dst) will copy a file to another file.

    Note that shutil.copyfile will not create directories that do not already exist. for that, use os.makedirs

    链接地址: http://www.djcxy.com/p/42316.html

    上一篇: Python:如何快速复制文件

    下一篇: 如何使用python复制文件?