Reading and Writing Files

Eventually you will have to at somepoint read and write to a file, Pythoin has a built-in function called open, this function returns an object to the file that then allows you to read/write from/to the file. The file object keep track of the file and how much has been written or read from the file. I will also cover pathlib and struct module

Open function

This section covers using the open function

Opening a file in read mode
import os
file_name = os.path.join("c:", "My Documents", "test", "myfile")

with open(file_name, 'r') as file_object:             # use a file name varaible, notice the r = read
    line = file_object.readline()

with open('myfile', 'r') as file_object:              # use direct filename, notice the r = read
    line = file_object.readline()
Closing a file
file_object = open("myfile", 'r')
# ... do something with file
file_object.close()

Note: its a good idea to close a file once finished with it as it releases any resources on that file
Opening a file in write mode
file_object = open("myfile", 'w')               # notice the w = write
Opening a file in binary mode
file_object = open("myfile", 'rb')               # notice the r = read, b = binary
file_object = open("myfile", 'wb')               # notice the w = write, b = binary
Reading from a file
file_object.readline()                          # read one line at a ime
file_object.readlines()                         # read all the lines in one go
Writing to a file
file_object.write("Hello")                      # write a single line
file_object.writelines(lines)                   # write many lines in one go

pathlib function

This section covers using the pathlib function

Opening a file
from pathlib import Path
>>> p_text = Path('my_text_file')

Note: you don't have to close a file with pathlib
Reading from a file
from pathlib import Path

p_text = Path('my_text_file')
text = p_text.read_text()                           # reading from a text file

p_text = Path('my_text_file')
p_text.read_bytes()                                 # reading from a binary file
Writing to a file
from pathlib import Path

p_text = Path('my_text_file')
p_text.write_text('Text file contents')             # writing to a text file

p_text = Path('my_text_file')
p_text.write_bytes(b'Text file contents')           # writing to a binary file, notice the b at the beginning of the text

Screen input

You can use the built-in input method to prompt for input from a user, or use the sys module

use input
x = input("enter file name to use: ")
x = int(input("enter your number: "))                 # you can convert the data into a specific data type, int in this case

Note: input is built-in in so nothing to import
use sys

import sys 

s = sys.stdin.readline()

Pickling Data

The pickle module allows you to save state (i.e variable), that then can be retrieved at a later date, for example you could save game data and when the game starts again it retrieves the saved data. Pickle can handle many types of data, int, strings, lists, tuples, dictionaries, even objects. Pickle serializes objects so they can be saved to a file and sent over a network as well, however it might be better in some circumstances to use JSON to store data.

Pickle Example
import pickle

a = 5
b = "paul"

## Save the data
file = open("state", 'wb')
pickle.dump(a, file)
pickle.dump(b, file)
file.close()

## Retrieve the data
file = open("state", 'rb')
a = pickle.load(file)
b = pickle.load(file)
file.close()

print(a)
print(b)

Shelving Objects

Shelving is a dictionary that stores data in a file instead of in memory like a dictionary, this means you can store very large amounts of data (more than you have memory), you can read/write to a shelve object withoput reading/writing the entire file. It actually uses the pickle module and can be used to create a database or a cache for example.

Shelving Example
import shelve

book = shelve.open("addresses")

book['flintstone'] = ('fred', '555-1234', '1233 Bedrock Place')
book['rubble'] = ('barney', '555-4321', '1235 Bedrock Place')

book.close()

book = shelve.open("addresses")
print( book['flintstone'] )
print( book['rubble'] )