如何使用python复制文件?

这个问题在这里已经有了答案:

  • 如何在python中复制文件? 14个答案

  • 要创建所有中间级目标目录,可以在复制之前使用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)
    

    看看shutilshutil.copyfile(src, dst)会将文件复制到另一个文件。

    请注意, shutil.copyfile不会创建不存在的目录。 为此,请使用os.makedirs

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

    上一篇: How to copy a file using python?

    下一篇: Redirect stdout to a file in Python?