Save a String to File in Python

Posted on August 26, 2019 at 12:23 pm

A simple way to save a string (or whatever) to a file in Python is:

file = open('File.txt', 'w')
file.write('whatever')
file.close()

An alternative method that auto-closed the file is:

with open('File.txt', 'w') as file:
    file.write('whatever')

Receive updates via email

Other Posts

Updated Posts