0

I'm following Zed A. Shaw's lpthw example 17 if you want to look at it https://learnpythonthehardway.org/book/ex17.html and it works just with only one line but not multiple (using terminal, windows powershell)
the original file says "This is a test you are being tested why does it not work on multiple lines the 2nd line says see but capitalized SEE" the 2nd file that copied the text and pasted it using write command says this "This is a test you are being tested why does it not work on multiple lines the 2nd line says see but capitalized ਍ऀ匀䔀䔀" I don't understand it i even copied his code and there isn't a single change in his or mine yet neither

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)

# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read()

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

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()`

I don't want to change the code much just because I know this works for a single line and beleive without much change it can work with an entire essay for example

  • 1
    Possible duplicate of [Python concatenate text files](http://stackoverflow.com/questions/13613336/python-concatenate-text-files) – Torxed Jan 12 '17 at 07:21
  • LPTHW is regarded as a sub-par tutorial, by the way. The official tutorial is much better. – TigerhawkT3 Jan 12 '17 at 07:54

2 Answers2

2

Try setting the correct encoding for the file you are reading when you open it open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

0

You may be having this problem due to the difference in which new lines are handled in Windows vs Linux. Have a look at : Handling \r\n vs \n newlines in python on Mac vs Windows

Community
  • 1
  • 1
Tapan Chugh
  • 354
  • 2
  • 4