Python writing binary files, bytes

后端 未结 3 1699
眼角桃花
眼角桃花 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

    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())
    

提交回复
热议问题