Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:21:34+00:00 2026-06-04T08:21:34+00:00

Following the excellent advice of a poster yesterday I started using the shutil.copyfileobj method

  • 0

Following the excellent advice of a poster yesterday I started using the shutil.copyfileobj method to make a copy of a file.

My program should make an exact copy of the file, remove the last byte and save the new copy.

I tested it last night with some very small ASCII text files so I could check it was doing what I asked it too, I have tried it this morning on some actual ‘complex’ files, a PDF and a JPG and it looks like the copy function is not making a true copy. I looked at the resulting files in a hex editor, and I can see that after ~ offset 0x300 there is something odd going – either data is being added, or data is being changed on copy. I can not tell which.

My program iteratively takes off a byte and saves a new version, and I can see that the newly created files are consistently different to original file, (with the exception of the last byte)

def doNibbleAndSave(srcfile,fileStripped,strippedExt,newpath):
 counter = '%(interationCounter)03d' % {"interationCounter":interationCounter} #creates the filename counter lable
 destfile = newpath + "\\" + fileStripped + "_" + counter + strippedExt #creates the new filename 
 with open(srcfile, 'r') as fsrc:
  with open(destfile, 'w+') as fdest:
   shutil.copyfileobj(fsrc, fdest)
   fdest.seek(nibbleSize, os.SEEK_END) #sets the number of bytes to be removed
   fdest.truncate()
 srcfile = destfile #makes the iterator pick up the newly 'nibbled' file to work on next
 return (srcfile)

I can also see that the newly created objects are significantly smaller than the source file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:21:35+00:00Added an answer on June 4, 2026 at 8:21 am

    As you already noticed, you should open the files in binary mode; open(srcfile, "rb") and open(destfile, "wb+"). Otherwise, Python will assume the files are text-files and may do newline conversion, depending on the platform (see the tutorial for details).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I
I'm using java 6 annotation processing api. I have followed the following excellent tutorial
Suppose I have the following file. John=good Tom = ok Tim = excellent I
Following Brad Wilson's excellent series on using and customizing editor templates , I tried
Following up on this excellent answer , I'm wondering if the DLR using the
In his excellent treatise on threading in C#, Joseph Albahari proposed the following simple
I'm reading through the excellent Ruby on Rails Tutorial and have encountered the following
I'm trying to connect to google documents (following Marco Cantu's excellent REST example) but
Following this JavaFX 2.0 tutorial I created a css file in eclipse that includes
Following the instructions here: http://www.padrinorb.com/guides/padrino-mailer I have the delivery method added on the app.rb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.