current position：Home>Write file operation of Python Foundation
Write file operation of Python Foundation
About bloggers ： Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python Knowledge of writing documents .
If you open a file , File opening mode with “b”, When writing the contents of the file ,str（ Parameters ） Use encode Method to byte stream , Otherwise, the report will be wrong .
write() Method is used to write the specified string to a file , The syntax is as follows ：
File object name .write(str)
example ： To file “testfile.txt” Write the following data in the file .
Options you shouldn’t use
file = open('testfile.txt','w') # Enter the string... Into the file file.write('Interface options\n') file.write('Generic options\n') file.write('Miscellaneous options\n') file.write('Options you shouldn’t use\n') file.close()
give the result as follows .
writelines() Method is used to write a sequence of strings to a file . This sequence of strings can be generated by an iterative object , Like a list of strings , The syntax is as follows ：
File object name .writelines(sequence)
example ： Use writelines() Method to existing “testfile.txt” The following data is added to the file .
ls = ['Environment\n','variables'] with open('testfile.txt','a') as file: file.writelines(ls) # Append a list of strings to the file
give the result as follows .
1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial
The above is about Python Knowledge of writing documents , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .
author[jiankang66],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Preliminary deployment of Django
- Getting started with Python flask
- Wavelet packet energy - Python code explanation
- Angie Angie parent-child tour, appreciate bamboo culture and visit giant pandas
- Python captures and cuts pictures from web pages
- [audio denoising] use VAD technology to clean up silent clips in WAV files (Python)
- OS and sys modules in Python
- [opencv] use the camera to perform face recognition detection in the pycharm + Anaconda environment (train to recognize multiple faces) - complete Python code is attached
- Use Python crawler +opencv to get the feedback data of the official interface through camera QR code recognition, so as to realize real-time and accurate detection of health code status (obtain JS dynamic data)
- How Python sets limits on memory and CPU usage
guess what you like
Python batch converts txt files to excel format
Understanding of sys.argv in Python
The ultimate solution to all kinds of errors in migrate in Python Django! Modify a table in the database (add new fields) and re-enter the online environment Django migrate to hot update the database without deleting records
Python master! Royal set artist of ue5
How to program in Python meets the following requirements
Python beginners have encountered some problems, as shown in the figure
Python implements emotion analysis
[Python foundation] decorator summary
[Python foundation] regular expression summary
[Python foundation] summary of socket network programming
- [Python foundation] multi process of multi task programming
- [Python foundation] multithreading of multitasking programming
- [Django] detailed explanation of Django configuration files and design patterns
- Those "pits" that you will step on in self-study Python
- Pandas query method in-depth summary
- Machine learning foundation of Python deep learning
- Python learning Chapter 1
- Python learning Chapter 2
- Python learning Chapter 3
- Python learning Chapter 4
- Python uses numpy to access files
- Python link MySQL database
- How to add a missing pyqt5 module (such as python3-pyqt5.qtultimedia)
- Python face recognition demo
- Python test code
- Python Programming: regular expression explanation
- Pandas learning
- [python + opencv] create videos from images
- The difference between Python nansum and nanbean multiplied by the number
- [Python advanced] network programming
- How to solve the problem of Python?
- Two methods of dividing one column into two columns in pandas
- [Python] Feature Engineering on cancer data to improve the accuracy of LR model (Feature Engineering and model aggregation)
- Python generate token
- Django database addition, deletion, modification and query
- Python file operation Encyclopedia
- Resolved (the urllib request page in python3 reports an error) attributeerror: module 'urllib' has no attribute 'request‘
- How to use Python to find out and replace the color patches of pictures?
- One line of Python code to merge dictionary and list
- [Python] found a useful data analysis tool
- Python uses SQLite database
- Python create e-Certificate
- Esp32 microprython driver st7735 1.8-inch TFT screen Chinese display
- Detailed explanation of the different usage of if x and if x is not none in Python
- Python object comparison and deep and shallow copy
- Value passing and reference passing in Python
- Multi thread and multi queue (Python)
- Whoosh: Python's lightweight search tool
- Python uses model Attributeerror:'nonetype'object has no attribute'compile' is encountered when compiling the method