Python writing binary files, bytes

后端 未结 3 1696
眼角桃花
眼角桃花 2020-12-11 15:30

Python 3. I\'m using QT\'s file dialog widget to save PDFs downloaded from the internet. I\'ve been reading the file using \'open\', and attempting to write it using the fil

相关标签:
3条回答
  • 2020-12-11 16:10

    If your intent is to simply make a copy of the file, you could use shutil

    >>> import shutil
    >>> shutil.copyfile('file_to_read.pdf','file_to_save.pdf')
    

    Or if you need to access byte by byte, similar to your structure, this works:

    >>> with open('/tmp/fin.pdf','rb') as f1:
    ...    with open('/tmp/test.pdf','wb') as f2:
    ...       while True:
    ...          b=f1.read(1)
    ...          if b: 
    ...             # process b if this is your intent   
    ...             n=f2.write(b)
    ...          else: break
    

    But byte by byte is potentially really slow.

    Or, if you want a buffer that will speed this up (without taking the risk of reading an unknown file size completely into memory):

    >>> with open('/tmp/fin.pdf','rb') as f1:
    ...    with open('/tmp/test.pdf','wb') as f2:
    ...       while True:
    ...          buf=f1.read(1024)
    ...          if buf: 
    ...              for byte in buf:
    ...                 pass    # process the bytes if this is what you want
    ...                         # make sure your changes are in buf
    ...              n=f2.write(buf)
    ...          else:
    ...              break
    

    With Python 2.7+ or 3.1+ you can also use this shortcut (rather than using two with blocks):

    with open('/tmp/fin.pdf','rb') as f1,open('/tmp/test.pdf','wb') as f2:
        ...
    
    0 讨论(0)
  • 2020-12-11 16:10

    It really doesn't make sense to write a file in another file. What you want is to write the contents of f1 in f2. You get the contents with f1.read(). So you have to do this:

    with open('file_to_read.pdf', 'rb') as f1: 
        with open('file_to_save.pdf', 'wb') as f2:
            f2.write(f1.read())
    
    0 讨论(0)
  • 2020-12-11 16:16

    learned from python cookbook

    from functools import partial
    
    with open(fpath, 'rb') as f, open(target_fpath, 'wb') as target_f: 
        for _bytes in iter(partial(f.read, 1024), ''):
            target_f.write(_bytes)
    

    partial(f.read, 1024) returns a function, read the binary file 1024 bytes at every turn. iter will end when meet a blank string ''.

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