Jun-09-2022, 03:22 PM
Hello! new user here, and a beginner Python student, studying with the book "Learn Python the Hard Way"
I reached an exercise where my script opens a txt file, reads the contents, copies them into a new file.
The problem I reached is this, in the new_file.txt my script creates, it includes these strange characters at the end of the text, which do not exist in the original txt file.
Please bear in mind this is my first post on the forum, if I did something wrong, I apologize!
My code:
![[Image: sePqcFv]](https://imgur.com/a/sePqcFv)
(previewing the post, it seems the image doesn't load, so I attach it as a file too just in case)
So, I'm trying to understand what causes these characters to appear in the second file, and what did I do wrong, if anything...
Thank you :)
I reached an exercise where my script opens a txt file, reads the contents, copies them into a new file.
The problem I reached is this, in the new_file.txt my script creates, it includes these strange characters at the end of the text, which do not exist in the original txt file.
Please bear in mind this is my first post on the forum, if I did something wrong, I apologize!
My code:
from sys import argv from os.path import exists script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") # we could do these two on one line, how? #in_file = open(from_file) ; indata = in_file.read() indata = open(from_file).read() print(f"The input file is {len(indata)} bytes long") print(f"Does the output file exist? {exists(to_file)}") print("Ready, hit RETURN to continue, CTRL-C to abort.") input() out_file = open(to_file, 'w') out_file.write(indata) print("Alright, all done") out_file.close() #in_file.close()The strange characters, as can be seen in VS Code, PowerShell and notepad:
(previewing the post, it seems the image doesn't load, so I attach it as a file too just in case)
So, I'm trying to understand what causes these characters to appear in the second file, and what did I do wrong, if anything...
Thank you :)