Size: 710
Comment:
|
Size: 904
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 14: | Line 14: |
The file object is used for reading and writing plain text as well as unformatted binary data. The following Codeline writes a message in the file with the name ''out.txt'' |
The [[http://docs.python.org/lib/bltin-file-objects.html|file object]] can be used for reading and writing plain text as well as unformatted binary data. The following code writes a message in the file with the name ''out.txt'', reads and print the data |
Line 17: | Line 18: |
file('out.txt','wb').write('Hallo Datentraeger') | file('out.txt','w').write('Hallo Datentraeger') print file('out.txt').read() |
Line 20: | Line 22: |
The data can be read in with the method '''.read()''' {{{#!python s=file('out.txt').read() print s 'Hallo Datentraeger' }}} |
* {{{write()}}} writes a string to the file * {{{read()}}} reads complete file * {{{read(N)}}} reads N bytes * {{{readlines()}}} reads the file with linebreaks * {{{readline()}}} reads only the next line |
Input/Output
This lesson deals with the ways of reading and writing data
Basic Python
The file object can be used for reading and writing plain text as well as unformatted binary data. The following code writes a message in the file with the name out.txt, reads and print the data
write() writes a string to the file
read() reads complete file
read(N) reads N bytes
readlines() reads the file with linebreaks
readline() reads only the next line
NumPy/SciPy
HDF
netCDF