with open('sample.txt', 'r') as f:
a = f.read()
with open('sample.txt', 'a') as f:
a = f.write('good girl')
print(f)
arju is the
# Open the file for reading
with open('sample.txt', 'r') as f:
content = f.read()
# Open the file for appending and add a newline character
with open('sample.txt', 'a') as f:
f.write('\ngood girl\n')
# Print the content of the file
print(content)
In Windows, you should use \r\n instead of just \n to represent a newline character.
This combination represents a carriage return followed by a line feed, which is the standard newline sequence in Windows text files. So, your code snippet should be modified accordingly:
# Open the file for reading
with open('sample.txt', 'r') as f:
content = f.read()
# Open the file for appending and add a Windows-style newline character
with open('sample.txt', 'a') as f:
f.write('\r\ngood girl\r\n')
# Print the content of the file
print(content)
Using os module in windows
import os
# Open the file for reading
with open('sample.txt', 'r') as f:
content = f.read()
# Open the file for appending and add a newline character
with open('sample.txt', 'a') as f:
f.write(os.linesep + 'good girl' + os.linesep)
# Print the content of the file
print(content)