How do I copy a file in Python?

前端 未结 16 2728
隐瞒了意图╮
隐瞒了意图╮ 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: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.

提交回复
热议问题