1

I'm trying to learn some programming by going through the exercises in Learn Python The Hard Way. I'm doing a little wrap-up exercise on my own by writing my own script, but unfortunately I have run into some problems related to printing files.

What I would like to achieve with my script is to:

  1. create a new file;
  2. copy the content from an existing file into that newly-created file;
  3. add some extra text by the user to the new file; and finally
  4. print the content of the new file.

I managed to achieve everything except the last bit: printing the new file. All I get is a blank space in the terminal window. The code is shown below:

v1 = raw_input("Specify which file to copy: ")

txt = open(v1).read()

print "This is the content of the file:"

print txt

v2 = raw_input("Choose a name for your new file, end it with txt")

nyfile = open(v2, "w+")
nyfile.write(txt)

nyfile.write(raw_input("Lets add some new text to the file: "))

txt2 = nyfile.read()

print txt2
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
MajkelB
  • 11
  • 2
  • The pointer is *at the end of the file* once you finish `write`ing - try using `txt2.seek(0)` to "rewind" back to the start first. – jonrsharpe Apr 07 '15 at 16:13
  • Thanks for the answer. It worked marvellously. Sorry for the duplicate... – MajkelB Apr 07 '15 at 16:48

0 Answers0