SlideShare a Scribd company logo
File-I/O

ist doch ganz einfach, oder?

Christian Kauhaus · kc@gocept.com

FLYING CIRCUS
let

y
yo u r w e b ap p f l
flyingcircus.io
with open(’users.json’, ’w’) as f:
json.dump(userdata, f)
File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?
Abstraktion
gut so
(meistens)
File-I/O -- ist doch ganz einfach, oder?
Kernelspace

Userspace

open(filename, mode,

Codec

encoding=...,

Universal newline

newline=...,

Buffer

Cache(s)
Filesystem

Disk

buffering=...)

os.open(filename, flags,
mode)
Atomarität
Illusion:
I/O geschieht
in einem Stück
with open(filename, ’wb’) as f:
f.write(data)

open("out", O_WRONLY|O_CREAT|O_TRUNC, 0666)
write(6, "1628 0
0
1"..., 1572864)
write(6, "232210H242276vn"..., 159416)
close(6)

=
=
=
=

6
1572864
159416
0
os.listdir(’/usr/lib’)

openat(AT_FDCWD, "/usr/lib", O_RDONLY|O_NONBLOCK|
O_DIRECTORY|O_CLOEXEC) = 3
getdents(3, /* 813 entries */, 32768)
= 32728
getdents(3, /* 809 entries */, 32768)
= 32744
getdents(3, /* 811 entries */, 32768)
= 32720
getdents(3, /* 657 entries */, 32768)
= 26424
getdents(3, /* 0 entries */, 32768)
= 0
close(3)
= 0
Problem?
with open(filename, ’rb’) as f:
data = f.read()
with open(filename, ’wb’) as f:
f.write(process(data))
with open(filename, ’rb’) as f:
data = f.read()
with tempfile.NamedTemporaryFile(
’wb’, dir=os.path.dirname(filename)) as t:
t.write(process(data))
tempname = t.name
os.rename(tempname, filename)
File-I/O -- ist doch ganz einfach, oder?
Persistenz
Illusion:
I/O greift direkt
auf die Disk zu
>>> f = open(’/tmp/out’, ’w’)
>>> print(’hello world’, file=f)
>>> os.system(’cat /tmp/out’)
0
>>> f.close()
>>> os.system(’cat /tmp/out’)
hello world
0
$ python write.py
$ ls -l out
-rw-r--r-- 1 ck users 5851 Okt 12 11:49 out
# system crash, reboot
$ ls -l out
-rw-r--r-- 1 ck users 0 Okt 12 11:49 out
Buffer leeren
Cache leeren
with open(filename, ’wb’) as f:
f.write(data)
f.flush()
os.fsync(f)
File-I/O -- ist doch ganz einfach, oder?
Text-Dateien
Illusion:
Dateien
enthalten
UnicodeCodepoints
>>> with open(filename) as f:
...
f.read()
UnicodeDecodeError: ’ascii’ codec can’t decode byte
0xc3 in position 1: ordinal not in range(128)
Implizite Codierung
# enctest.py
print(’preferred encoding:’,
locale.getpreferredencoding())
with open(’preferred’, ’w’) as f:
f.write(’hëllon’)
print(’default encoding’,
sys.getdefaultencoding())
with open(’default’, ’wb’) as f:
f.write(’hëllon’.encode())
$ export LC_ALL=de_DE # latin1 encoding
$ python3.2 enctest.py
preferred encoding: ISO-8859-1
default encoding utf-8
$ ls -l preferred default
-rw-r--r-- 1 ck users 6 Okt 12 12:52 preferred
-rw-r--r-- 1 ck users 7 Okt 12 12:52 default
explizit
with open(filename, ’w’, encoding=’utf-8’) as f:
f.write(data)
Fazit
File-I/O ist einfach
Grenzen der
Abstraktion kennen
File-I/O -- ist doch ganz einfach, oder?
Fragen?
Bildnachweis

© zweiwest.ch

flickr.com/teachernz
CC BY-NC-SA

flickr.com/iaea_imagebank
CC BY-NC-ND

flickr.com/atlantica
CC BY

flickr.com/jasoneppink
CC BY-NC-SA

flickr.com/seychelles88
CC BY-NC-SA

More Related Content

DOC
How to send files to remote server via ssh in php
PDF
言語の設計判断
PDF
Client-side Storage 
 
PPT
01 linux basics
ODP
NUMOSS 4th Week - Commandline Tutorial
PDF
Git installation
How to send files to remote server via ssh in php
言語の設計判断
Client-side Storage 
 
01 linux basics
NUMOSS 4th Week - Commandline Tutorial
Git installation

What's hot (20)

PPT
Shell and perl scripting classes in mumbai
ODP
Linux Command Line
PPTX
자바스터디 4
PDF
Neoito — *NIX kungfu for web devs
PDF
Beyond Golden Containers: Complementing Docker with Puppet
TXT
How to install
PDF
Python & FUSE
DOCX
Jose dossantos.doc
TXT
Mounting usb free bsd
ODP
Presentasi Mac'LC
ODP
Presentasi mac'lc-02
TXT
TXT
C99[2]
PDF
Pry at the Ruby Drink-up of Sophia, February 2012
PPT
Unix Programming with Perl
PPTX
Using the Power to Prove
PDF
Workshop programs
PDF
Combine vs RxSwift
PDF
How to stand on the shoulders of giants
PDF
Clase4 (consola linux)
Shell and perl scripting classes in mumbai
Linux Command Line
자바스터디 4
Neoito — *NIX kungfu for web devs
Beyond Golden Containers: Complementing Docker with Puppet
How to install
Python & FUSE
Jose dossantos.doc
Mounting usb free bsd
Presentasi Mac'LC
Presentasi mac'lc-02
C99[2]
Pry at the Ruby Drink-up of Sophia, February 2012
Unix Programming with Perl
Using the Power to Prove
Workshop programs
Combine vs RxSwift
How to stand on the shoulders of giants
Clase4 (consola linux)
Ad

Viewers also liked (8)

PPT
Triad 2010 word_chapter_4
PDF
backy - Image-basiertes Backup für virtuelle Maschinen
PPT
Triad 2010 power_point_chapter_1
PPTX
Evaluation[1]
PPT
Institutional research
PDF
CATALOGO MOBIAL EQUIPAMENTOS INTEGRALES
PPTX
Q[1]
PPS
Chandresh Engineering Works
Triad 2010 word_chapter_4
backy - Image-basiertes Backup für virtuelle Maschinen
Triad 2010 power_point_chapter_1
Evaluation[1]
Institutional research
CATALOGO MOBIAL EQUIPAMENTOS INTEGRALES
Q[1]
Chandresh Engineering Works
Ad

Similar to File-I/O -- ist doch ganz einfach, oder? (10)

PDF
5. Ввод-вывод, доступ к файловой системе
PDF
Making Mongo realtime - oplog tailing in Meteor
PDF
Let's Go-lang
PDF
7 strace examples to debug the execution of a program in linux
PPTX
File management
PPT
Unit5
PDF
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
PPT
7.0 files and c input
PPT
Java 7
5. Ввод-вывод, доступ к файловой системе
Making Mongo realtime - oplog tailing in Meteor
Let's Go-lang
7 strace examples to debug the execution of a program in linux
File management
Unit5
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
7.0 files and c input
Java 7

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
Teaching material agriculture food technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Cloud computing and distributed systems.
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
Teaching material agriculture food technology
Unlocking AI with Model Context Protocol (MCP)
Cloud computing and distributed systems.
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation

File-I/O -- ist doch ganz einfach, oder?