5 – File I/O, Plotting with Matplotlib
5 ? 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):
The readlines() method returns a list of the lines in the file:
for line in fp: print(line)
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
txt = fp.read()
entire file content as one string:
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
- matplotlib tutorialspoint
- matplotlib a tutorial
- matplotlib university of massachusetts amherst
- 1 4 matplotlib plotting
- using matplotlib bucknell university
- matplotlib 2d and 3d plotting in python
- python printing formats file writing twoways
- plotting and visualization obviously awesome
- lab 5 introduction to matplotlib
- chapter plotting data using 4 matplotlib ncert
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