Skip to content

Latest commit

 

History

History

17-File-IO

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

File I/O - Input/Output

  • Really useful for data science - csv files, datasets, etc.
  • Useful for web dev - reading html/css/js files and sending contents

Reading Files

  • We can read a file by using the open(file_name) function
  • open returns a file object to us - there's some metadata about the file
  • Then we have to read it using the read() method
  • open has a bunch of optional parameters but the file name is a compulsory
  • If file can't be opened, an OSError is raised
  • We can ref. either absolute or relative path

There are a bunch of options which can are there on the python documentation

# open a file object
f = open(file_name.txt)

# print file object (instance of TextIOWrapper)
print(f)

# print the contents of the file
print(f.read())

Cursor Movement

When you use a read method on a file for the second time, you'll see an empty string. This is because python reads it using the cursor movement. So once you read the whole file, the cursor moves at the very end. When you again try to read the file, there's nothing left after the cursor from the last time.

seek

used to send the python cursor to a specific place in the file

file = open(file_name.txt)

file.seek(0) # will send it back to the start of the file

file.seek(2) # will send the cursor to the index 2

readline

  • Used to only read a line
  • Useful when reading a large file line by line
file = open(file_name.txt)

file.readline() # read the first line (till `.`)

file.readline() # read the next line

readlines

  • Reads all lines one by one and puts them in a list
file = open(file_name.txt)

file.readlines() # returns a list of all the different lines

Closing Files

When we're done using a file we should close a file so we don't waste resources.

file.closed - returns if the file is closed or not (True/False) file.close() - closes the file

The with statement

Another way to open and read python files but using this statement automatically closes the files so we don't have to manually close them.

with open(file_name.txt) as file:
  file.read()

file.closed # will always return True, we don't have to do it

Behind the scenes, with uses the __enter__ and __exit__ method to initiate and close a file every time it is called no matter what.

Writing Files

  • We still have to use open function to write to a file
  • Have to specify -w flag to specify that we're writing
with open('file_name.txt', 'w') as file:
  file.write("I am writing this...Wow")
  • The write method doesn't preserve, it rewrites everything
  • If the file doesn't exist, python will create a new one with the specified name

File Modes

There are a bunch of different flags (called modes) that we can pass along with the open function. They can be seen on the documentation.

  • r - read a file (default)
  • w - write to a file (overwriting)
  • a - append to a file (no overwriting)
  • r+ - read & write to a file (writing based on cursor)
    • common to use with files with pre-existing data
    • only woks with pre-existing files won't create one for us
    • throws error if file isn't found