Python - Writing to Files(Novice)

Hello Freecodecamp,

I’m following the 4.5hr Beginner youtube tutorial on python & came across an issue.

I have the exact same code & file structure as the tutorial, with the only difference being that the tutorial is using Pycharm & I am using Visual Studio.

Thing is that when I run my code to append the employees.txt file code shown below, the string is added to the last line in the employees.txt file–not added to a new line. I can get around it using the “\n”…but I was wondering if this is an update to python or is it because I am using a code editor vs an IDE. ??

## My code Using "a" append 

employee_file = open("employees.txt", "a")

employee_file.write("\nToby - Human Resources")


## code

employee_file = open("employees.txt", "a")

employee_file.write("Toby - Human Resources")


When Python appends to a text file, it appends to the end of the text file, including on an already written line. So, the \n is needed if you want to create a new line for your new string.

OK, Much appreciated. The tutorial I was following looked like it was appending to the next line, & I thought I was doing something wrong.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.