Based on EX17 problem

I’m very new to coding and the book has done wonders for me so far.

After completing ex17 I decided to give myself a challenge based on it, basically I wanted to open a file, copy the contents of it by storing it in a variable, ask the user for input, store that in a variable and rewrite the destination file by adding these two variables to it.

Here is my code:

print("Copying a file to add it to another file!")
from sys import argv
from os.path import exists #Technicaly don't need that here(?)

script, from_file, to_file = argv
from_file = open(from_file)
print("Your file contents:")
contents =
print("Is there anything you would like to add to the destination file? \n(leave empty if not)... ")
contents2 = input("")

to_file = open(to_file, 'w+')
to_file.write(contents + contents2)

#Now I want to read what I've done
print("Let's read our new file:") 


The problem is it doesn’t seem to save the contents of the from_file from this:

  contents =

Even though it does print it when I gave it the command to before

(BTW is the output in the shell suppose to be all specey? it also gives me weird characters I never put in there ( ÿþ - to be specific ))

So when I finally tell it to print the new file it just prints me the contents of contents2 without contents .

I get no error but the program doesn’t work like I planned it. I realize I may have made a mess of things so I also included the output that I got in the Shell:

> echo "This is a test file"> test.txt
> python test.txt new_testfor17.txt
Copying a file to add it to another file!
Your file contents:
ÿþT h i s   i s   a   t e s t   f i l e

Is there anything you would like to add to the destination file?
(leave empty if not)...
Let's read our new file: 

Thank you!

I’m not sure if I am one to be trying to help lol but I took a swag at your question.
I read, copied and ran your code. The 'w+' did stand out to me. not saying its wrong, just I did not recall what it does. Did some googling of your question\problem (which I repeatedly see questions answered with something like “you need to google that”) and after three different answer this page gave me answer.

I don’t recall the other sources, but basically changed ‘w’ to, ‘a’,
then added to_file = open(to_file, 'r') after to_file.write(contents + contents2) and promptly deleted it lol.

Then tried 'a' and 'a+'.
'a+' seems to do the trick.
Added note, it seems it will write to last place the cursor was. So if only one line in the .txt file it will write on the end. If two lines but second is empty will write there.

Never mind on the cursor placement, seems to place text on last available line.

@dragonazz ahhh thankyou sm for replying!! :star: :star:

I think @dragonazz has a good answer, but the reason your file is messed up is you probably made it with my advice of doing it on the command line, but your computer is setup to create Unicode or a similar encoding and it made a non-ASCII file.

Delete that file and try making it with your text editor.

1 Like