Saturday, May 15, 2021

Python File Objects

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 
after using it to free up resources; else, Python will destroy the object and 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 
return an empty string '' 

>>> 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() 

>>>  

 
`file.readlines()` will read all lines by storing each line to a list. 

>>> 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 
number of characters written. 

>>> 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) 
or in an opaque number (for files open in text mode). 

>>> f.read() 

'1st line\n2nd line\n' 

>>> f.tell() 

18 

>>>  

 
To change the file object's position, use `file.seek(offset, from_what)`. Position is computed 
by adding an `offset` from the refernce point `from_what`. Reference points are "0" for 
beginning of file, "1" for current position, and "2" for end of file. 

>>> 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' 
 
So if you want to read again from the beginning, do this: 

>>> f.read() 

'1st line\n2nd line\n' 

>>> f.seek(0,0) 

0 

>>> f.read() 

'1st line\n2nd line\n' 

Best practice in 
dealing with file objects 

Using `with` will automatically close the file for you which is the recommended way of 
dealing with file objects. 

>>> with open('/tmp/test.txt') as f: 

...   f.read() 

...  

'1st line\n2nd line' 

>>> f.closed 

True 

Replacing a line 
persistently 

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