Commit ee25f4a5 authored by Franck Thollard's avatar Franck Thollard
Browse files

using consistent fstring

parent 829b6221
......@@ -153,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -171,7 +171,7 @@
"source": [
"with open('../pyfiles/helloworld.py') as file:\n",
" for i, line in enumerate(file):\n",
" print(f'line {i:2d}: ' + line.strip())"
" print(f'line {i:2d}: {line.strip()}')"
]
},
{
......@@ -297,7 +297,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.2+"
}
},
"nbformat": 4,
......
%% Cell type:markdown id: tags:
# Python training UGA 2017
**A training to acquire strong basis in Python to use it efficiently**
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)
# Read / write files
%% Cell type:markdown id: tags:
## Read / write files
There are a lot of specialized tools to open specialized files (images, xml, csv, hdf5, netcdf, etc.). Here we focus on the low-level general method to open files.
### [`open` built-in function](https://docs.python.org/3/library/functions.html#open) and file object
%% Cell type:code id: tags:
``` python
file = open('../pyfiles/helloworld.py')
txt = file.read()
file.close()
print(txt)
```
%%%% Output: stream
print('Hello world')
name = 'Pierre'
print('My name is ' + name)
%% Cell type:markdown id: tags:
But what if something weird happens when the file is open (e.g. a division by O) ?
-> Exception is raised that could be catch and run some code that is not aware of the opening
file.
-> The file remains open
%% Cell type:markdown id: tags:
### Context, `with` keyword
For such objects that need to be closed, it is a good practice to use the keyword `with` (THIS IS MUCH BETTER than using the close function, USE `with`!). Like this, we are sure that the file will be closed even if there is an error:
%% Cell type:code id: tags:
``` python
with open('../pyfiles/helloworld.py') as file:
txt = file.read()
print(txt)
```
%%%% Output: stream
print('Hello world')
name = 'Pierre'
print('My name is ' + name)
%% Cell type:markdown id: tags:
### Loop over lines
%% Cell type:code id: tags:
``` python
with open('../pyfiles/helloworld.py') as file:
for line in file:
print(f'line ?: ' + line.strip())
```
%%%% Output: stream
line ?:
line ?: print('Hello world')
line ?:
line ?: name = 'Pierre'
line ?: print('My name is ' + name)
%% Cell type:markdown id: tags:
And now using `enumerate` to get the index of the line:
%% Cell type:code id: tags:
``` python
with open('../pyfiles/helloworld.py') as file:
for i, line in enumerate(file):
print(f'line {i:2d}: ' + line.strip())
print(f'line {i:2d}: {line.strip()}')
```
%%%% Output: stream
line 0:
line 1: print('Hello world')
line 2:
line 3: name = 'Pierre'
line 4: print('My name is ' + name)
%% Cell type:markdown id: tags:
### Options of the built-in function open (read, write, append)
%% Cell type:code id: tags:
``` python
# write data in a file
with open('../TP/zoo.txt', 'w') as file_zoo:
file_zoo.write('sam;cat;2\n')
file_zoo.write('liloo;lion;2\n')
with open('../TP/zoo.txt', 'a') as file_zoo:
file_zoo.write('peter;panda;5\n')
with open('../TP/zoo.txt') as file_zoo:
print(file_zoo.read())
```
%%%% Output: stream
sam;cat;2
liloo;lion;2
peter;panda;5
%% Cell type:code id: tags:
``` python
with open('../TP/zoo.txt', 'r') as file_zoo:
print(file_zoo.readline())
print(file_zoo.read())
```
%%%% Output: stream
sam;cat;2
liloo;lion;2
peter;panda;5
%% Cell type:markdown id: tags:
Remark: difference `write` and `print`.
%% Cell type:markdown id: tags:
### Options of the built-in function open (binary file)
Until now, we have only written text files. It can of course be much more efficient to use binary format.
%% Cell type:code id: tags:
``` python
with open('/tmp/test', 'wb') as file:
file.write(b'a')
```
%% Cell type:markdown id: tags:
Remarks:
- In practice, saving data in binary file is most of the time a bad idea. There are much better solutions to do this (see for example h5py and h5netcdf).
- There are Python libraries to read and process many types for files (csv, xml, json, images, tabulars, etc.).
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment