5 – File I/O, Plotting with Matplotlib
5 ¨C File I/O, Plotting with Matplotlib
B¨¢lint Aradi
Course: Scientific Programming / Wissenchaftliches Programmieren (Python)
Installing some SciPy stack components
We will need several Scipy components for the exercises:
sudo apt-get install python3-scipy python3-matplotlib
2
File I/O workflow
¡ñ
Open file
¡ñ
Do read/write operations
¡ñ
Close file
fp = open("test.txt", "r")
txt = fp.read()
fp.close()
¡ñ
¡ñ
¡ñ
The closing of a file is optional (although recommended)
Using context manager blocks (with ¡ as ¡) closing the file can be
automatic
File would be closed as soon as the block is left
with open("test.txt", "r") as fp:
txt = fp.read()
print("The file has been already closed")
3
Opening a file
¡ñ
A file is opened by the open() function
open(filename, mode)
¡ñ
It returns a file handler which can be used to manipulate the file content
¡ñ
The file handler is valid until the file is closed with the close() statement
¡ñ
Mode flag determines what can be done with the file and how the file
content is handled (as text or binary data)
"r"
Open for reading (default)
"w"
Open for writing (truncating content if already present)
"a"
Open for writing (appending to existing content)
"b"
Binary mode
"t"
Text mode (default)
"+"
Open file for updating (reading and writing)
4
Reading from text file
fp = open("test.txt", "r")
¡ñ
¡ñ
¡ñ
¡ñ
Iterating over file handler
returns the lines in the file as
strings (including the newline
character a the line ends):
for line in fp:
print(line)
The readlines() method returns a
list of the lines in the file:
lines = fp.readlines()
print(lines)
The readline() method returns the
next line in the file (and empty
string if all lines had been read):
line = fp.readline()
while line:
print(line)
line = fp.readline()
The read() method returns the
entire file content as one string:
txt = fp.read()
print(txt)
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- if you have to process difficult characters utf 8 encoding and sas
- sas and utf 8 ultimately the finest your data and applications will
- programming with unicode documentation read the docs
- lab manual python programming lab 6cs4 23 jnit
- enc portable tools for utf 8 character data
- python lesson 6 fileinput and output github pages
- oracle outside in technology 8 5 6 supported formats january 2022
- release 0 8 read the docs
- reading and writing data files with python astrophysics
- sas 9 3 utf 8 encoding support and related issue troubleshooting
Related searches
- i work well with others
- i will be with you hymn
- i need help with college tuition
- i need help with credit card debt
- words i can make with letters
- i m 55 with no savings
- file income tax free with irs
- 5 letter word that start with a
- file i o in java
- am i in love with him quiz
- words i can make with these letter
- words i can spell with letters