How do I copy a file in Python?

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

    Python provides in-built functions for easily copying files using the Operating System Shell utilities.

    Following command is used to Copy File

    shutil.copy(src,dst)
    

    Following command is used to Copy File with MetaData Information

    shutil.copystat(src,dst)
    
    0 讨论(0)
  • 2020-11-21 07:52

    Firstly, I made an exhaustive cheatsheet of shutil methods for your reference.

    shutil_methods =
    {'copy':['shutil.copyfileobj',
              'shutil.copyfile',
              'shutil.copymode',
              'shutil.copystat',
              'shutil.copy',
              'shutil.copy2',
              'shutil.copytree',],
     'move':['shutil.rmtree',
             'shutil.move',],
     'exception': ['exception shutil.SameFileError',
                     'exception shutil.Error'],
     'others':['shutil.disk_usage',
                 'shutil.chown',
                 'shutil.which',
                 'shutil.ignore_patterns',]
    }
    

    Secondly, explain methods of copy in exmaples:

    1. shutil.copyfileobj(fsrc, fdst[, length]) manipulate opened objects
    In [3]: src = '~/Documents/Head+First+SQL.pdf'
    In [4]: dst = '~/desktop'
    In [5]: shutil.copyfileobj(src, dst)
    AttributeError: 'str' object has no attribute 'read'
    #copy the file object
    In [7]: with open(src, 'rb') as f1,open(os.path.join(dst,'test.pdf'), 'wb') as f2:
        ...:      shutil.copyfileobj(f1, f2)
    In [8]: os.stat(os.path.join(dst,'test.pdf'))
    Out[8]: os.stat_result(st_mode=33188, st_ino=8598319475, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067347, st_mtime=1516067335, st_ctime=1516067345)
    
    1. shutil.copyfile(src, dst, *, follow_symlinks=True) Copy and rename
    In [9]: shutil.copyfile(src, dst)
    IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
    #so dst should be a filename instead of a directory name
    
    1. shutil.copy() Copy without preseving the metadata
    In [10]: shutil.copy(src, dst)
    Out[10]: ~/desktop/Head+First+SQL.pdf'
    #check their metadata
    In [25]: os.stat(src)
    Out[25]: os.stat_result(st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066425, st_mtime=1493698739, st_ctime=1514871215)
    In [26]: os.stat(os.path.join(dst, 'Head+First+SQL.pdf'))
    Out[26]: os.stat_result(st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066427, st_mtime=1516066425, st_ctime=1516066425)
    # st_atime,st_mtime,st_ctime changed
    
    1. shutil.copy2() Copy with preseving the metadata
    In [30]: shutil.copy2(src, dst)
    Out[30]: ~/desktop/Head+First+SQL.pdf'
    In [31]: os.stat(src)
    Out[31]: os.stat_result(st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067055, st_mtime=1493698739, st_ctime=1514871215)
    In [32]: os.stat(os.path.join(dst, 'Head+First+SQL.pdf'))
    Out[32]: os.stat_result(st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067063, st_mtime=1493698739, st_ctime=1516067055)
    # Preseved st_mtime
    
    1. shutil.copytree()

    Recursively copy an entire directory tree rooted at src, returning the destination directory

    0 讨论(0)
  • 2020-11-21 07:52
    open(destination, 'wb').write(open(source, 'rb').read())
    

    Open the source file in read mode, and write to destination file in write mode.

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

    You could use os.system('cp nameoffilegeneratedbyprogram /otherdirectory/')

    or as I did it,

    os.system('cp '+ rawfile + ' rawdata.dat')
    

    where rawfile is the name that I had generated inside the program.

    This is a Linux only solution

    0 讨论(0)
提交回复
热议问题