How do I copy a file in Python?

前端 未结 16 2214
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:12

How do I copy a file in Python?

I couldn\'t find anything under os.

相关标签:
16条回答
  • 2020-11-21 07:41

    copy2(src,dst) is often more useful than copyfile(src,dst) because:

    • it allows dst to be a directory (instead of the complete target filename), in which case the basename of src is used for creating the new file;
    • it preserves the original modification and access info (mtime and atime) in the file metadata (however, this comes with a slight overhead).

    Here is a short example:

    import shutil
    shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') # complete target filename given
    shutil.copy2('/src/file.ext', '/dst/dir') # target filename is /dst/dir/file.ext
    
    0 讨论(0)
  • 2020-11-21 07:41

    You can use one of the copy functions from the shutil package:

    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    Function              preserves     supports          accepts     copies other
                          permissions   directory dest.   file obj    metadata  
    ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
    shutil.copy              ✔             ✔                 ☐           ☐
    shutil.copy2             ✔             ✔                 ☐           ✔
    shutil.copyfile          ☐             ☐                 ☐           ☐
    shutil.copyfileobj       ☐             ☐                 ✔           ☐
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    

    Example:

    import shutil
    shutil.copy('/etc/hostname', '/var/tmp/testhostname')
    
    0 讨论(0)
  • 2020-11-21 07:41

    Directory and File copy example - From Tim Golden's Python Stuff:

    http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html

    import os
    import shutil
    import tempfile
    
    filename1 = tempfile.mktemp (".txt")
    open (filename1, "w").close ()
    filename2 = filename1 + ".copy"
    print filename1, "=>", filename2
    
    shutil.copy (filename1, filename2)
    
    if os.path.isfile (filename2): print "Success"
    
    dirname1 = tempfile.mktemp (".dir")
    os.mkdir (dirname1)
    dirname2 = dirname1 + ".copy"
    print dirname1, "=>", dirname2
    
    shutil.copytree (dirname1, dirname2)
    
    if os.path.isdir (dirname2): print "Success"
    
    0 讨论(0)
  • 2020-11-21 07:43

    shutil has many methods you can use. One of which is:

    from shutil import copyfile
    copyfile(src, dst)
    
    • Copy the contents of the file named src to a file named dst.
    • The destination location must be writable; otherwise, an IOError exception will be raised.
    • If dst already exists, it will be replaced.
    • Special files such as character or block devices and pipes cannot be copied with this function.
    • With copy, src and dst are path names given as strings.

    If you use os.path operations, use copy rather than copyfile. copyfile will only accept strings.

    0 讨论(0)
  • 2020-11-21 07:43

    For small files and using only python built-ins, you can use the following one-liner:

    with open(source, 'rb') as src, open(dest, 'wb') as dst: dst.write(src.read())
    

    As @maxschlepzig mentioned in the comments below, this is not optimal way for applications where the file is too large or when memory is critical, thus Swati's answer should be preferred.

    0 讨论(0)
  • 2020-11-21 07:49
    from subprocess import call
    call("cp -p <file> <file>", shell=True)
    
    0 讨论(0)
提交回复
热议问题