append write python

with open(filename, "a+") as f:
  f.write('Hello World')

Here is what the above code is Doing:
1. Opening the file in append mode.
2. Writing the string ‘Hello World’ to the file.
3. Closing the file.

The file will now contain the string ‘Hello World’ at the end of it.

You can also open a file in read mode, write mode, or read/write mode.

Here’s how you would open a file in read mode:

filename = ‘file.txt’

with open(filename, ‘r’) as f:
print(f.read())

The file will now be open in read mode and you can read its contents.

Here’s how you would open a file in write mode:

filename = ‘file.txt’

with open(filename, ‘w’) as f:
f.write(‘Hello World’)

The file will now be open in write mode and you can write to it.

Here’s how you would open a file in read/write mode:

filename = ‘file.txt’

with open(filename, ‘r+’) as f:
f.write(‘Hello World’)

The file will now be open in read/write mode and you can read from it and write to it.