Introduction
------------
- reading and writing files is done via open()
- open() returns a file object
- syntax:
open('filename', 'mode')
where modes are:
r - open file for reading (default)
w - open file for writing (content will be overwritten)
a - append
r+ - read and write
- some basic operations on a file: `file.write()` and `file.read()`
- when opening a binary file, append "b" in mode
- when reading a file, line endings (\n on Unix, \n\r on Windows) are converted
to \n
- when writing in text mode, \n is converted back to platform-specific line
endings which may corrupt a binary data when it is opened in text mode
Daling with file objects
------------------------
'r' = open for reading (default)
'w' = open for writing, truncating the file first
'x' = open for exclusive creation, failing if the file already exists
'a' = open for writing, appending to the end of the file if it exists
'b' = binary mode
't' = text mode (default)
'+' = open a disk file for updating (reading and writing)
'U' = universal newlines mode (deprecated)
Reading | This opens the file for reading and closes it afterwards. It is advisable to close the file for you but that may take some time.
This reads the entire content of a file. If the end of file has been reached, f.read() will >>> f = open('/tmp/test.txt') >>> f.read() 'hello python\n' >>> f.read() '' >>> f.close() >>> f.closed True >>>
To read a single line, use `file.readline()` >>> f = open('test') >>> f.readline() '1st line\n' >>> f.readline() '2nd line\n' >>> f.close() >>> >>> f = open('test') >>> print(f.readlines()) ['1st line\n', '2nd line\n'] >>>
This is a memory efficient way of reading a file. >>> f = open('test') >>> for line in f: ... print(line, end='') ... 1st line 2nd line >>> |
Writing and appending | Writing to a file will overwrite its content (if there is any). It will also return the >>> f = open('/tmp/test.txt', 'w') >>> f.write('This will overwrite the content') 31 >>> f.close() >>> f = open('/tmp/test.txt') >>> f.read() 'This will overwrite the content' >>> f.close() >>>
If the file doesn't exist, it will be created. >>> newfile = open('/tmp/newfile', 'w') >>> from subprocess import call >>> call(["ls", "-l", "/tmp/newfile"]) -rw-r--r-- 1 merrell merrell 0 Aug 14 20:45 /tmp/newfile 0 >>>
Appending is as easy as `a`. >>> f = open('/tmp/test.txt', 'a') >>> f.write('This will be appended') 21 >>> f.close() >>> f = open('/tmp/test.txt') >>> f.read() 'This will overwrite the contentThis will be appended' # notice line termination is incorrect >>> f.close() >>> |
Positioning | `file.tell()` returns the file's current position in bytes (for files open in binary mode) >>> f.read() '1st line\n2nd line\n' >>> f.tell() 18 >>> >>> f = open('workfile', 'rb+') >>> f.write(b'0123456789abcdef') 16 >>> f.seek(5) # Go to the 6th byte in the file 5 >>> f.read(1) b'5' >>> f.seek(-3, 2) # Go to the 3rd byte before the end 13 >>> f.read(1) b'd' >>> f.read() '1st line\n2nd line\n' >>> f.seek(0,0) 0 >>> f.read() '1st line\n2nd line\n' |
Best practice in | Using `with` will automatically close the file for you which is the recommended way of >>> with open('/tmp/test.txt') as f: ... f.read() ... '1st line\n2nd line' >>> f.closed True |
Replacing a line | for line in fileinput.input(['thefile.txt'], inplace=True): print(line.replace('old stuff', 'shiny new stuff'), end='') |
Checks if file is a directory | os.path.isdir('/tmp/sample/dir1') |
No comments:
Post a Comment