User Program Communication

Write to File in Python

Used to create and write characters to a file.


Syntax
#import the io library which opens the file
import io

output_file = open("target_filename", "w")
output_file.write(content)
output_file.close()

Notes

The default path is the source file's path. The current working directory can be retrieved using the 'getcwd()' method, from the os package.


Example
instructions = open("instructions.txt", "w")
instructions.write("To use the site, either search for your topic or select a language and concept.")
instructions.close()

< Read from Text File   |   Import Module >

© 2019 SyntaxDB. All Rights Reserved.