0

I have nailed down the issue to a single line. out_file.read() is not outputting the file's data correctly, why? Here my file again and some comments where the problem is coming from.

#How to copy data from one file into another

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

from_data = open(from_file, "r").read()

print "The input file is %d bytes long" % len(from_data)

print "Checking if output file exist..... %r" % exists(to_file)
print "Hit return to continue or Cntl C to quit"

#keyboard input
raw_input()

out_file = open(to_file, "w+")
out_file.write(from_data)

print "Okay all done, your new file now contains:"
#Using out_file.read() doesnt work as expected.
#Special chars get printed out and a bunch of 
#nulls get appended to the end of the actual to_file file 
#along with the copied content. Why is this?
#print out_file.read()

out_file.close()
DEdesigns57
  • 361
  • 1
  • 5
  • 13
  • OP's [previous question](http://stackoverflow.com/questions/28503401/whats-wrong-with-this-method-for-copying-a-file-in-python) for reference – Cory Kramer Feb 13 '15 at 18:38

1 Answers1

3

you must go back to the beginning

out_file.seek(0) #back up to the begining of the file
print out_file.read() #read in all the file contents from where ever the cursor is (at the beginning now)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179