In this article we will use the Python constructor open to write strings to TXT files.

See the demo directly. Py

Text = "Sample text to Save \nNew Line" " Open Opens or creates a file, and if examplefile.txt does not exist, it automatically creates a w that indicates a writable mode, 'r' ''' saveFile = open(' examplefile.txt ', 'w') savefile.write (text) savefile.close () # If your demo.py file is on your desktop, So examplefile.txt is also created on the Desktop if you want to specify a specific path you can say saveFile = open('C:\Users\Anthony\Desktop\ examplefile.txt ', 'w') ".Copy the code

There is a small problem here, if I want to input Chinese to TXT file will report unicode error, can not be solved for the moment.

This article is from the SDK community: www.sdk.cn