How do I copy a file in Python?

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

    As of Python 3.5 you can do the following for small files (ie: text files, small jpegs):

    from pathlib import Path
    
    source = Path('../path/to/my/file.txt')
    destination = Path('../path/where/i/want/to/store/it.txt')
    destination.write_bytes(source.read_bytes())
    

    write_bytes will overwrite whatever was at the destination's location

提交回复
热议问题