Autosave: 2024-10-25 09:51:23

This commit is contained in:
thomasabishop 2024-10-25 09:51:23 +01:00
parent 9fc5b54fe7
commit 2cfe56d95a
753 changed files with 349 additions and 225 deletions

View file

@ -9,7 +9,7 @@ The responsibility for enabling communication between nodes fell to the hosts,
not the [IMPs](e470bf3d_IMPs_in_the_ARPANET.md). At the lowest level (equivalent
to the [link layer](Link_Layer_of_Internet_Protocol.md) in the subsequent
internet?), this consisted in moving raw bits between hosts regardless of what
specific type of data encoded.
specific type of data they encoded.
In the book (_Where Wizards Stay Up Late_), there is a great description by
analogy:

View file

@ -0,0 +1,18 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# Appending to files in Python
```py
file = open("example.txt", "a")
file.write('Add something to the end of the file')
file.close()
```
```py
with open("example.txt", "a"):
file.write("Add something to the end of the file")
file.close()
```

View file

@ -0,0 +1,24 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# File CRUD operations in Python
Most create, delete, move etc operations invoke the inbuilt `os` module.
// Add directory CRUD operations in Python
## Renaming files (moving)
```py
import os
os.rename('original-file-name.txt', 'new-file-name.txt')
```
## Deleting files
```py
import os
os.remove('file-name.txt')
```

View file

@ -0,0 +1,20 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# File system error handling in Python
The [error handler](Error_handling_in_Python.md) in the case of a file not being
found:
```py
try:
with open('filename.txt', 'r') as file:
contents = file.readlines()
for line in lines:
print(line)
except FileNotFoundError as err:
print("File does not exist")
print(err)
```

View file

@ -1,36 +1,9 @@
---
tags: [python]
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# IO in Python
## The open() object
The built-in `open()` function creates a **file object** that allows us to read,
write and append to files.
The general syntax is as follows:
```py
file_object = open(<file_name>, <access_mode>)
```
`<file_name>` is obviously a path to the file you want to read, create or
modify. The `<access_mode>` denotes the mode in which to open the file. The most
frequently used are:
- `r`
- read
- `w`
- write
- `a`
- append
### All access modes
In addition we have the following access modes
## Reading files
# Reading files in Python
Once a file object has been intialised with `open()` there are several ways in
which the content can be read:
@ -41,7 +14,7 @@ which the content can be read:
| `readline` | Read the contents of a file a line at a time. You would combine this with a loop so that you can do something with each individual line. |
| `readlines` | Return a list of all the lines in a file. Each line will be an element in the list. |
### Read
## Read
`read` reads the entire contents of a file and returns it as a single string.
@ -77,7 +50,7 @@ for line in lines:
# do something with line
```
### Readline
## Readline
> The readline() method in Python is used to read a single line from a file. It
> is typically used when you want to process a file line by line, rather than
@ -107,7 +80,7 @@ while line:
file.close()
```
### Readlines
## Readlines
The `readlines()` method is used to read all the lines of a file and return them
as a list of strings, where each element of the list is a line from the file.
@ -125,76 +98,5 @@ for line in lines:
# Close the file
file.close()
```
### Error handling
Obviously file access can raise errors - typically when the file you want to
access does not exist (i.e. a `FileNotFoundError`
[exception](Error_handling_in_Python.md)). We can manage this scenario with
[exception handlers](Error_handling_in_Python.md):
```py
try:
with open('filename.txt', 'r') as file:
contents = file.readlines()
for line in lines:
print(line)
except FileNotFoundError as err:
print("File does not exist")
print(err)
```
## Close and "with as"
You notice that once we have finished with our I/O operation, we must call
`file.close()` to terminate the process. This removes the reference to the file
from memory.
A more pythonic and concise way of reading files and closing them is to use
`with...as` syntax. When this phrasing is used, a self-contained context is
created for the I/O operation that closes the file automatically.
```py
with open('filename.txt', 'r') as file:
contents = file.read()
print(contents)
```
## Writing to files
Again we create a file object with `open()` and this time use the `write`
method:
```py
# Open file in write mode
file = open("example.txt", "w")
# Write some text to the file
file.write("Hello, this is an example text written using Python.")
# Close the file
file.close()
```
> Note that in the above example, if the file does not already exist, it will
> create it. If it does exist, it will overwrite its contents with the new data.
> So we use `write` to create new files as well as to write to existing files.
## Renaming and deleting files
We have to use another built-in module to rename and delete files: `os`.
To rename an existing file:
```py
import os
os.rename('original-file-name.txt', 'new-file-name.txt')
```
To delete a file:
```py
import os
os.remove('file-name.txt')
`
```

View file

@ -0,0 +1,40 @@
---
tags: [python]
created: Friday, October 25, 2024
---
# With open in Python
We use the `open()` method to create a **file object** that allows us to read,
write and append to files.
The general syntax is as follows:
```py
file_object = open(<file_name>, <access_mode>)
```
`<file_name>` is a path to the file you want to read, create or modify. The
`<access_mode>` denotes the mode in which to open the file. The most frequently
used are:
- `r`
- [read](Reading_files_in_Python.md)
- `w`
- [write](Writing_to_files_in_Python.md)
- `a`
- [append](Appending_to_files_in_Python.md)
When we have finished with an I/O operation, such as reading from or writing to
a file, we must call `file.close()` to terminate the process. This removes the
reference to the file from memory.
A more pythonic and concise way of reading files and closing them is to use
`with...as` syntax. When this phrasing is used, a self-contained context is
created for the I/O operation that closes the file automatically.
```py
with open('filename.txt', 'r') as file:
contents = file.read()
print(contents)
```

View file

@ -0,0 +1,32 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# Writing to files in Python
We create a file object with `open()` and use the `write` method:
```py
# Open file in write mode
file = open("example.txt", "w")
# Write some text to the file
file.write("Hello, this is an example text written using Python.")
# Close the file
file.close()
```
Alternatively we use `with open` which automatically closes the file:
```py
with open("example.txt", "w") as file
file.write('some lines')
```
> Note that in the above example, if the file does not already exist, it will
> create it. If it does exist, it will overwrite its contents with the new data.
> So we use `write` to create new files as well as to write to existing files.

View file

@ -0,0 +1,23 @@
---
tags: [ARPANET, networks, computer-history]
created: Friday, October 25, 2024
---
# c8173d17_TIMPs
After the initial ARPANET was complete, the next major milestone was to enable
access to the network regardless of one's proximity to a host node with an IMP
connection.
The idea was to allow a computer to access resources on another computer
directly without connecting to a host first. This connective computer would
connect to an IMP directly (but still transparently) as a 'dumb terminal' as
with time-sharing and would not be a fully-fledged computing device. These
devices would be called _Terminal_ Information Processors (TIMPs) for this
reason.
The development of TIMPs makes it clearer that the host machines on the ARPANET
were not just connection and transmission nodes for their own purpose, they were
loci for other non-host computers to gain access to an IMP, and thereby, the
broader network. In other words computers would connect to a host which
sustained a connection to an IMP.

View file

@ -1,5 +1,5 @@
---
tags: [ARPA, ARPANET, networks, computer-history]
tags: [ARPANET, networks, computer-history]
created: Friday, October 18, 2024
---

View file

@ -13,23 +13,23 @@ computer science.
![not-by-ai-badge](static/not-by-ai-alternative--light.svg)
**Build ID:** 22009d2d-5405-4213-9214-3625c42630dd
**Build ID:** 34679fdf-5a10-42d3-941d-d835c4b29c36
**Published:** Thu 24 Oct 2024 20:15:17
**Published:** Fri 25 Oct 2024 09:51:21
### Recent edits
- [[653c0b1d_host_protocols_of_ARPANET]]
- [[cfbef1c4_web_precursors]]
- [[Reading_files_in_Python]]
- [[File_CRUD_operations_in_Python]]
- [[With_open_in_Python]]
- [[Writing_to_files_in_Python]]
- [[Error_handling_in_Python]]
- [[File_system_error_handling_in_Python]]
- [[Appending_to_files_in_Python]]
- [[e470bf3d_IMPs_in_the_ARPANET]]
- [[f0f70a3c_ARPANET_motivations]]
- [[Threads]]
- [[385af4b4_Baran_distributed_networks]]
- [[861cc26e_ARPA_origins]]
- [[Compile_Python_app_to_single_executable]]
### All notes (467)
### All notes (473)
- [[385af4b4_Baran_distributed_networks]]
- [[653c0b1d_host_protocols_of_ARPANET]]
@ -51,6 +51,7 @@ computer science.
- [[Any]]
- [[Apollo_Client]]
- [[Apollo_Server]]
- [[Appending_to_files_in_Python]]
- [[Application_Layer_of_Internet_Protocol]]
- [[Application_state_management_with_React_hooks]]
- [[Application_structure]]
@ -184,8 +185,10 @@ computer science.
- [[Factory_pattern]]
- [[Fetch_decode_execute]]
- [[Fetch_from_Secrets_Manager]]
- [[File_CRUD_operations_in_Python]]
- [[File_descriptors]]
- [[File_permissions_and_execution_in_Bash]]
- [[File_system_error_handling_in_Python]]
- [[Filesystems]]
- [[Find_Bash_command]]
- [[Flip_flops]]
@ -222,7 +225,6 @@ computer science.
- [[Headless_Raspi_network_setup]]
- [[Heap_memory]]
- [[Hexadecimal_number_system]]
- [[IO_in_Python]]
- [[IP_addresses]]
- [[Identify_merged_branches]]
- [[Importing_MongoDB_data]]
@ -367,6 +369,7 @@ computer science.
- [[React_useReducer]]
- [[React_useState]]
- [[Read_command__in_Bash]]
- [[Reading_files_in_Python]]
- [[Reciprocals]]
- [[Recursion]]
- [[Redirect_to_dev_null]]
@ -480,11 +483,14 @@ computer science.
- [[Why_computers_use_binary]]
- [[Wildcards_in_SQL]]
- [[Williams_Tube_memory]]
- [[With_open_in_Python]]
- [[Working_with_CSVs_in_Python]]
- [[Working_with_JSON_in_Python]]
- [[Working_with_numbers_in_Bash]]
- [[Writing_to_files_in_Python]]
- [[Zero_property_of_multiplication]]
- [[Zip_function_in_Python]]
- [[c8173d17_TIMPs]]
- [[cfbef1c4_web_precursors]]
- [[e470bf3d_IMPs_in_the_ARPANET]]
- [[f0f70a3c_ARPANET_motivations]]

View file

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

View file

Before

Width:  |  Height:  |  Size: 145 KiB

After

Width:  |  Height:  |  Size: 145 KiB

View file

Before

Width:  |  Height:  |  Size: 289 KiB

After

Width:  |  Height:  |  Size: 289 KiB

View file

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View file

Before

Width:  |  Height:  |  Size: 6.9 MiB

After

Width:  |  Height:  |  Size: 6.9 MiB

View file

Before

Width:  |  Height:  |  Size: 224 KiB

After

Width:  |  Height:  |  Size: 224 KiB

View file

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

View file

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View file

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View file

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 204 KiB

After

Width:  |  Height:  |  Size: 204 KiB

View file

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 49 KiB

View file

Before

Width:  |  Height:  |  Size: 165 KiB

After

Width:  |  Height:  |  Size: 165 KiB

View file

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View file

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

View file

Before

Width:  |  Height:  |  Size: 133 KiB

After

Width:  |  Height:  |  Size: 133 KiB

View file

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View file

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View file

Before

Width:  |  Height:  |  Size: 4 KiB

After

Width:  |  Height:  |  Size: 4 KiB

View file

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View file

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View file

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 6.3 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 148 KiB

After

Width:  |  Height:  |  Size: 148 KiB

View file

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

View file

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View file

Before

Width:  |  Height:  |  Size: 9.6 KiB

After

Width:  |  Height:  |  Size: 9.6 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 121 KiB

After

Width:  |  Height:  |  Size: 121 KiB

View file

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View file

Before

Width:  |  Height:  |  Size: 470 KiB

After

Width:  |  Height:  |  Size: 470 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View file

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 59 KiB

View file

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 59 KiB

View file

Before

Width:  |  Height:  |  Size: 99 KiB

After

Width:  |  Height:  |  Size: 99 KiB

View file

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

View file

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View file

Before

Width:  |  Height:  |  Size: 848 KiB

After

Width:  |  Height:  |  Size: 848 KiB

View file

Before

Width:  |  Height:  |  Size: 194 KiB

After

Width:  |  Height:  |  Size: 194 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 130 KiB

After

Width:  |  Height:  |  Size: 130 KiB

View file

Before

Width:  |  Height:  |  Size: 171 KiB

After

Width:  |  Height:  |  Size: 171 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View file

Before

Width:  |  Height:  |  Size: 9.7 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

View file

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View file

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 79 KiB

View file

Before

Width:  |  Height:  |  Size: 157 KiB

After

Width:  |  Height:  |  Size: 157 KiB

View file

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 72 KiB

View file

Before

Width:  |  Height:  |  Size: 153 KiB

After

Width:  |  Height:  |  Size: 153 KiB

View file

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

View file

Before

Width:  |  Height:  |  Size: 87 KiB

After

Width:  |  Height:  |  Size: 87 KiB

View file

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 118 KiB

View file

Before

Width:  |  Height:  |  Size: 414 KiB

After

Width:  |  Height:  |  Size: 414 KiB

View file

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 167 KiB

After

Width:  |  Height:  |  Size: 167 KiB

View file

Before

Width:  |  Height:  |  Size: 150 KiB

After

Width:  |  Height:  |  Size: 150 KiB

View file

Before

Width:  |  Height:  |  Size: 9.2 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

View file

Before

Width:  |  Height:  |  Size: 140 KiB

After

Width:  |  Height:  |  Size: 140 KiB

View file

Before

Width:  |  Height:  |  Size: 190 KiB

After

Width:  |  Height:  |  Size: 190 KiB

View file

Before

Width:  |  Height:  |  Size: 374 KiB

After

Width:  |  Height:  |  Size: 374 KiB

View file

Before

Width:  |  Height:  |  Size: 9 KiB

After

Width:  |  Height:  |  Size: 9 KiB

View file

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View file

Before

Width:  |  Height:  |  Size: 76 KiB

After

Width:  |  Height:  |  Size: 76 KiB

View file

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 122 KiB

View file

Before

Width:  |  Height:  |  Size: 668 KiB

After

Width:  |  Height:  |  Size: 668 KiB

View file

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View file

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View file

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 41 KiB

View file

Before

Width:  |  Height:  |  Size: 108 KiB

After

Width:  |  Height:  |  Size: 108 KiB

View file

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 37 KiB

View file

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View file

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View file

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 66 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View file

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View file

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View file

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

View file

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View file

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

View file

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View file

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View file

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

View file

Before

Width:  |  Height:  |  Size: 7.3 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB

View file

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View file

Before

Width:  |  Height:  |  Size: 146 KiB

After

Width:  |  Height:  |  Size: 146 KiB

View file

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View file

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View file

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 63 KiB

View file

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

View file

@ -9,7 +9,7 @@ The responsibility for enabling communication between nodes fell to the hosts,
not the [IMPs](e470bf3d_IMPs_in_the_ARPANET.md). At the lowest level (equivalent
to the [link layer](Link_Layer_of_Internet_Protocol.md) in the subsequent
internet?), this consisted in moving raw bits between hosts regardless of what
specific type of data encoded.
specific type of data they encoded.
In the book (_Where Wizards Stay Up Late_), there is a great description by
analogy:

View file

@ -0,0 +1,18 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# Appending to files in Python
```py
file = open("example.txt", "a")
file.write('Add something to the end of the file')
file.close()
```
```py
with open("example.txt", "a"):
file.write("Add something to the end of the file")
file.close()
```

View file

@ -0,0 +1,24 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# File CRUD operations in Python
Most create, delete, move etc operations invoke the inbuilt `os` module.
// Add directory CRUD operations in Python
## Renaming files (moving)
```py
import os
os.rename('original-file-name.txt', 'new-file-name.txt')
```
## Deleting files
```py
import os
os.remove('file-name.txt')
```

View file

@ -0,0 +1,20 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# File system error handling in Python
The [error handler](Error_handling_in_Python.md) in the case of a file not being
found:
```py
try:
with open('filename.txt', 'r') as file:
contents = file.readlines()
for line in lines:
print(line)
except FileNotFoundError as err:
print("File does not exist")
print(err)
```

View file

@ -1,36 +1,9 @@
---
tags: [python]
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# IO in Python
## The open() object
The built-in `open()` function creates a **file object** that allows us to read,
write and append to files.
The general syntax is as follows:
```py
file_object = open(<file_name>, <access_mode>)
```
`<file_name>` is obviously a path to the file you want to read, create or
modify. The `<access_mode>` denotes the mode in which to open the file. The most
frequently used are:
- `r`
- read
- `w`
- write
- `a`
- append
### All access modes
In addition we have the following access modes
## Reading files
# Reading files in Python
Once a file object has been intialised with `open()` there are several ways in
which the content can be read:
@ -41,7 +14,7 @@ which the content can be read:
| `readline` | Read the contents of a file a line at a time. You would combine this with a loop so that you can do something with each individual line. |
| `readlines` | Return a list of all the lines in a file. Each line will be an element in the list. |
### Read
## Read
`read` reads the entire contents of a file and returns it as a single string.
@ -77,7 +50,7 @@ for line in lines:
# do something with line
```
### Readline
## Readline
> The readline() method in Python is used to read a single line from a file. It
> is typically used when you want to process a file line by line, rather than
@ -107,7 +80,7 @@ while line:
file.close()
```
### Readlines
## Readlines
The `readlines()` method is used to read all the lines of a file and return them
as a list of strings, where each element of the list is a line from the file.
@ -125,76 +98,5 @@ for line in lines:
# Close the file
file.close()
```
### Error handling
Obviously file access can raise errors - typically when the file you want to
access does not exist (i.e. a `FileNotFoundError`
[exception](Error_handling_in_Python.md)). We can manage this scenario with
[exception handlers](Error_handling_in_Python.md):
```py
try:
with open('filename.txt', 'r') as file:
contents = file.readlines()
for line in lines:
print(line)
except FileNotFoundError as err:
print("File does not exist")
print(err)
```
## Close and "with as"
You notice that once we have finished with our I/O operation, we must call
`file.close()` to terminate the process. This removes the reference to the file
from memory.
A more pythonic and concise way of reading files and closing them is to use
`with...as` syntax. When this phrasing is used, a self-contained context is
created for the I/O operation that closes the file automatically.
```py
with open('filename.txt', 'r') as file:
contents = file.read()
print(contents)
```
## Writing to files
Again we create a file object with `open()` and this time use the `write`
method:
```py
# Open file in write mode
file = open("example.txt", "w")
# Write some text to the file
file.write("Hello, this is an example text written using Python.")
# Close the file
file.close()
```
> Note that in the above example, if the file does not already exist, it will
> create it. If it does exist, it will overwrite its contents with the new data.
> So we use `write` to create new files as well as to write to existing files.
## Renaming and deleting files
We have to use another built-in module to rename and delete files: `os`.
To rename an existing file:
```py
import os
os.rename('original-file-name.txt', 'new-file-name.txt')
```
To delete a file:
```py
import os
os.remove('file-name.txt')
`
```

40
zk/With_open_in_Python.md Normal file
View file

@ -0,0 +1,40 @@
---
tags: [python]
created: Friday, October 25, 2024
---
# With open in Python
We use the `open()` method to create a **file object** that allows us to read,
write and append to files.
The general syntax is as follows:
```py
file_object = open(<file_name>, <access_mode>)
```
`<file_name>` is a path to the file you want to read, create or modify. The
`<access_mode>` denotes the mode in which to open the file. The most frequently
used are:
- `r`
- [read](Reading_files_in_Python.md)
- `w`
- [write](Writing_to_files_in_Python.md)
- `a`
- [append](Appending_to_files_in_Python.md)
When we have finished with an I/O operation, such as reading from or writing to
a file, we must call `file.close()` to terminate the process. This removes the
reference to the file from memory.
A more pythonic and concise way of reading files and closing them is to use
`with...as` syntax. When this phrasing is used, a self-contained context is
created for the I/O operation that closes the file automatically.
```py
with open('filename.txt', 'r') as file:
contents = file.read()
print(contents)
```

View file

@ -0,0 +1,32 @@
---
tags: [python, file-system, procedural]
created: Friday, October 25, 2024
---
# Writing to files in Python
We create a file object with `open()` and use the `write` method:
```py
# Open file in write mode
file = open("example.txt", "w")
# Write some text to the file
file.write("Hello, this is an example text written using Python.")
# Close the file
file.close()
```
Alternatively we use `with open` which automatically closes the file:
```py
with open("example.txt", "w") as file
file.write('some lines')
```
> Note that in the above example, if the file does not already exist, it will
> create it. If it does exist, it will overwrite its contents with the new data.
> So we use `write` to create new files as well as to write to existing files.

23
zk/c8173d17_TIMPs.md Normal file
View file

@ -0,0 +1,23 @@
---
tags: [ARPANET, networks, computer-history]
created: Friday, October 25, 2024
---
# c8173d17_TIMPs
After the initial ARPANET was complete, the next major milestone was to enable
access to the network regardless of one's proximity to a host node with an IMP
connection.
The idea was to allow a computer to access resources on another computer
directly without connecting to a host first. This connective computer would
connect to an IMP directly (but still transparently) as a 'dumb terminal' as
with time-sharing and would not be a fully-fledged computing device. These
devices would be called _Terminal_ Information Processors (TIMPs) for this
reason.
The development of TIMPs makes it clearer that the host machines on the ARPANET
were not just connection and transmission nodes for their own purpose, they were
loci for other non-host computers to gain access to an IMP, and thereby, the
broader network. In other words computers would connect to a host which
sustained a connection to an IMP.

View file

@ -1,5 +1,5 @@
---
tags: [ARPA, ARPANET, networks, computer-history]
tags: [ARPANET, networks, computer-history]
created: Friday, October 18, 2024
---