How to save text files in python

How to save text files in python:

Use python's built-in open() class to open the text file, write data to the file, use the write() function, after writing, use the close() function to close and save the text file

The sample code is as follows:

The results are as follows:

Content expansion:

Python3 saves data as a txt file, the specific content is as follows:

f =open("data/model_Weight.txt",'a') #If the file does not exist, the system automatically creates it.'a'Indicates that it can be written to the file continuously, and the original content is retained.
  # Write the content later. This mode can be modified ('w+','w','wb'Wait)
 
f.write("hello,sha") #Write string to file
f.write("\n")   #Wrap
if __name__=='__main__':
 fw =open("/exercise1/data/query_deal.txt",'w') #The address of the file to be saved
 for line inopen("/exercise1/data/query.txt"): #File read
 fw.write("\"poiName\":\""+ line.rstrip("\n")+"\"") #Write string to file
 # line.rstrip("\n")To remove end-of-line newlines
 fw.write("\n") #Wrap

So far, this article on how to save text files in python is introduced. For more related methods of saving text files in python, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. Cn!

Recommended Posts

How to save text files in python
How to delete files and directories in python
How to save IE as an attachment in python
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to use the zip function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to install the downloaded module in python
How to write a confession program in python
How to read and write files with Python
How does Python store data to json files
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
3 ways to encrypt Python files
How to understand python objects
How to use python tuples
How to create a Python virtual environment in Ubuntu 14.04
Python implements FTP to upload files in a loop
What are the ways to open files in python
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
python how to view webpage code
How to use hanlp in ubuntu
How to use python thread pool