Read and write a file

The contents of the text file test.txt AAA BBB CCC DDD If

python


s = """\
AAA
BBB
DDD
DDD
"""
with open ('test.txt', 'r+') as f:
    print(f.read())
    f.seek(0)
    f.write(s)

Execution result


AAA
BBB
CCC
DDD

The contents of the text file test.txt are AAA BBB DDD DDD become.

r + is read + write. So If you don't have test.txt Unable to read An error will occur.

Recommended Posts

Read and write a file
Write and read a file
Read and write csv file
datetime and file write and backup
Read file
Write standard output to a file
[Note] Read a file from another directory
Bind to class to read and write YAML
[GO language] Let's read a YAML file
Write O_SYNC file in C and Python
Read and write csv files with numpy
Read and write JSON files in Python
Read a character data file with numpy
[python] Read html file and practice scraping
Read and format a csv file mixed with comma tabs with Python pandas
How to read a CSV file with Python 2/3
Read a file containing garbled lines in Python
[Python3] Read and write with datetime isoformat with json
Let's write a Python program and run it
Read line by line from a file with Python
I want to write to a file with Python
How to read a file in a different directory
[Introduction to Pandas] Read a csv file without a column name and give it a column name
I want to write an element to a file with numpy and check it.
[Python] Concatenate a List containing numbers and write it to an output file.
Fourier transform the wav file read by Python, reverse transform it, and write it again.
File read overhead measurement
How to read a serial number file in a loop, process it, and graph it
Read Python csv file
Try creating a compressed file using Python and zlib
Python-Read data from a numeric data file and calculate covariance
File write speed comparison experiment between python 2.7.9 and pypy 2.5.0
Read the csv file and display it in the browser
Read and write single precision floating point in Python
Read and write files with Slackbot ~ Bot development with Python ~
Read json file with Python, format it, and output json
Read and write NFC tags in python using PaSoRi
Output a binary dump in binary and revert to a binary file
[Python] Start a batch file from Python and pass variables.
Read the csv file with jupyter notebook and write the graph on top of it
Save the pystan model and results in a pickle file
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Device and Linux file system
Write a kernel density function
[Python] Read the csv file and display the figure with matplotlib
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
Upload a file to Dropbox
A1 notation and 26-ary number
Read / write between BigQuery ↔ Pandas
Create a dummy data file
Linux file and directory permissions
Python> Read from a multi-line string instead of a file> io.StringIO ()
Read the VTK file and display the color map with jupyter.
Export a gzip-compressed text file
[Python] Use this to read and write wav files [wavio]
How to write a metaclass that supports both python2 and python3
Difference between ps a and ps -a
Beginners add disks and use them as a file system (´ ・ ω ・ `)
Create a temporary file with django as a zip file and return it
Process Splunk execution results using Python and save to a file
How to make a command to read the configuration file with pyramid