Write and read a file

1


s = """\
AAA
BBB
CCC
DDD
"""
with open ('test.txt', 'w') as f:
    f.write(s)

with open ('test.txt', 'r') as f:
    print(f.read())

Execution result of 1


AAA
BBB
CCC
DDD

this

2


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

Can be written. After writing, the index will be the last, so

python


f.seek(0)

You need to go back to the beginning with.

w + is Write + read.

python


with open ('test.txt', 'w+') as f:
    print(f.read())

Then First go into write mode Once empty, Do not write anything Because it will be read Nothing is output.

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
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
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
Python --Read data from a numeric data file and find the multiple regression line.
I want to write an element to a file with numpy and check it.
Read CSV file: pandas
[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
Created a module to monitor file and URL updates
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 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)
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
Read zipline brand data from csv file and perform back test
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
Read a file in Python with a relative path from the program