This article mainly introduces Python3 common methods for writing files, analyzes the conventional methods for writing files and improvement methods in details in the form of instances, as well as the techniques for writing files in segments, for more information about how to write data to a file in Python3, see the following example. Share it with you for your reference. The details are as follows:
'''''Created on Dec 18,201 2 write to a file @ author: liury_lab ''' # The simplest method is all_the_text = 'Hello Python' open ('d:/text.txt ', 'W '). write (all_the_text) all_the_data = B 'abcd1234' open ('d:/data.txt ', 'wb '). write (all_the_data) # better way file_object = open ('d:/text.txt ', 'w') file_object.write (all_the_text) file_object.close () # write list_of_text_strings = ['hello ', 'python', 'hello', 'World'] file_object = open ('d:/text.txt ', 'w') for string in list_of_text_strings: file_object.writelines (string) list_of_text_strings = ['hello', 'Python', 'hello', 'World'] file_object = open ('d:/text.txt ', 'w') file_object.writelines (list_of_text_strings)
I hope this article will help you with Python programming.