Working with files¶
See content for switch.cfg
Reading a file¶
Show help for open(). Discuss input arguments (file and mode)
Open a file:
Read the file:
Read the file again to show that now the content is empty, this is because you have removed it from the buffer or exhausted it.
Close the file:
Writing to a file¶
Use the following dictionary:
vlans = [
{'id': '10', 'name': 'USERS'},
{'id': '20', 'name': 'VOICE'},
{'id': '30', 'name': 'WLAN'},
{'id': '40', 'name': 'APP'},
{'id': '50', 'name': 'WEB'}
]
Print the dictionary with json:
Open a file for writing:
Show that the file was created.
Write to a file:
Show that the file is still empty.
Close the file:
Show the file that there is a content now.
The with statement¶
Open the file with the with statement: