Autosave: 2024-10-25 09:51:23
|
|
@ -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:
|
||||
|
|
@ -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()
|
||||
```
|
||||
|
|
@ -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')
|
||||
```
|
||||
|
|
@ -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)
|
||||
```
|
||||
|
|
@ -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')
|
||||
`
|
||||
```
|
||||
|
|
@ -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)
|
||||
```
|
||||
|
|
@ -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.
|
||||
|
|
@ -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.
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
tags: [ARPA, ARPANET, networks, computer-history]
|
||||
tags: [ARPANET, networks, computer-history]
|
||||
created: Friday, October 18, 2024
|
||||
---
|
||||
|
||||
|
|
@ -13,23 +13,23 @@ computer science.
|
|||
|
||||

|
||||
|
||||
**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]]
|
||||
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 82 KiB |
|
Before Width: | Height: | Size: 7.1 KiB After Width: | Height: | Size: 7.1 KiB |
|
Before Width: | Height: | Size: 145 KiB After Width: | Height: | Size: 145 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 68 KiB |
|
Before Width: | Height: | Size: 289 KiB After Width: | Height: | Size: 289 KiB |
|
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 6.9 MiB After Width: | Height: | Size: 6.9 MiB |
|
Before Width: | Height: | Size: 224 KiB After Width: | Height: | Size: 224 KiB |
|
Before Width: | Height: | Size: 6.4 KiB After Width: | Height: | Size: 6.4 KiB |
|
Before Width: | Height: | Size: 312 KiB After Width: | Height: | Size: 312 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 724 KiB After Width: | Height: | Size: 724 KiB |
|
Before Width: | Height: | Size: 198 KiB After Width: | Height: | Size: 198 KiB |
|
Before Width: | Height: | Size: 232 KiB After Width: | Height: | Size: 232 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 3.6 MiB After Width: | Height: | Size: 3.6 MiB |
|
Before Width: | Height: | Size: 204 KiB After Width: | Height: | Size: 204 KiB |
|
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 49 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 165 KiB After Width: | Height: | Size: 165 KiB |
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 5.3 KiB After Width: | Height: | Size: 5.3 KiB |
|
Before Width: | Height: | Size: 90 KiB After Width: | Height: | Size: 90 KiB |
|
Before Width: | Height: | Size: 91 KiB After Width: | Height: | Size: 91 KiB |
|
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 133 KiB |
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 61 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 4 KiB After Width: | Height: | Size: 4 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 6.3 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 194 KiB After Width: | Height: | Size: 194 KiB |
|
Before Width: | Height: | Size: 148 KiB After Width: | Height: | Size: 148 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 9 KiB After Width: | Height: | Size: 9 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 9.6 KiB After Width: | Height: | Size: 9.6 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 70 KiB |
|
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 121 KiB |
|
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
|
Before Width: | Height: | Size: 808 KiB After Width: | Height: | Size: 808 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
|
Before Width: | Height: | Size: 470 KiB After Width: | Height: | Size: 470 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 99 KiB |
|
Before Width: | Height: | Size: 201 KiB After Width: | Height: | Size: 201 KiB |
|
Before Width: | Height: | Size: 175 KiB After Width: | Height: | Size: 175 KiB |
|
Before Width: | Height: | Size: 299 KiB After Width: | Height: | Size: 299 KiB |
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 213 KiB After Width: | Height: | Size: 213 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 9.6 KiB After Width: | Height: | Size: 9.6 KiB |
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
|
Before Width: | Height: | Size: 155 KiB After Width: | Height: | Size: 155 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 99 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 848 KiB After Width: | Height: | Size: 848 KiB |
|
Before Width: | Height: | Size: 194 KiB After Width: | Height: | Size: 194 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 56 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 142 KiB After Width: | Height: | Size: 142 KiB |
|
Before Width: | Height: | Size: 194 KiB After Width: | Height: | Size: 194 KiB |
|
Before Width: | Height: | Size: 130 KiB After Width: | Height: | Size: 130 KiB |
|
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 171 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 47 KiB After Width: | Height: | Size: 47 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 9.7 KiB After Width: | Height: | Size: 9.7 KiB |
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 51 KiB |
|
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 79 KiB |
|
Before Width: | Height: | Size: 95 KiB After Width: | Height: | Size: 95 KiB |
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
|
Before Width: | Height: | Size: 157 KiB After Width: | Height: | Size: 157 KiB |
|
Before Width: | Height: | Size: 226 KiB After Width: | Height: | Size: 226 KiB |
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 72 KiB |
|
Before Width: | Height: | Size: 153 KiB After Width: | Height: | Size: 153 KiB |
|
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 52 KiB |
|
Before Width: | Height: | Size: 145 KiB After Width: | Height: | Size: 145 KiB |
|
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 87 KiB |
|
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 74 KiB |
|
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 414 KiB After Width: | Height: | Size: 414 KiB |
|
Before Width: | Height: | Size: 314 KiB After Width: | Height: | Size: 314 KiB |
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 167 KiB After Width: | Height: | Size: 167 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 150 KiB After Width: | Height: | Size: 150 KiB |
|
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 69 KiB |
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 50 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
|
Before Width: | Height: | Size: 93 KiB After Width: | Height: | Size: 93 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 41 KiB |
|
Before Width: | Height: | Size: 9.2 KiB After Width: | Height: | Size: 9.2 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 73 KiB |
|
Before Width: | Height: | Size: 65 KiB After Width: | Height: | Size: 65 KiB |
|
Before Width: | Height: | Size: 47 KiB After Width: | Height: | Size: 47 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 140 KiB After Width: | Height: | Size: 140 KiB |
|
Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 75 KiB |
|
Before Width: | Height: | Size: 123 KiB After Width: | Height: | Size: 123 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
|
Before Width: | Height: | Size: 190 KiB After Width: | Height: | Size: 190 KiB |
|
Before Width: | Height: | Size: 374 KiB After Width: | Height: | Size: 374 KiB |
|
Before Width: | Height: | Size: 9 KiB After Width: | Height: | Size: 9 KiB |
|
Before Width: | Height: | Size: 1.2 MiB After Width: | Height: | Size: 1.2 MiB |
|
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 99 KiB |
|
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 76 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 96 KiB After Width: | Height: | Size: 96 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 122 KiB After Width: | Height: | Size: 122 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 668 KiB After Width: | Height: | Size: 668 KiB |
|
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 1.3 MiB |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 70 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 43 KiB After Width: | Height: | Size: 43 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 9.7 KiB After Width: | Height: | Size: 9.7 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 52 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 8.9 KiB After Width: | Height: | Size: 8.9 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 108 KiB After Width: | Height: | Size: 108 KiB |
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 41 KiB |
|
Before Width: | Height: | Size: 108 KiB After Width: | Height: | Size: 108 KiB |
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 50 KiB |
|
Before Width: | Height: | Size: 90 KiB After Width: | Height: | Size: 90 KiB |
|
Before Width: | Height: | Size: 144 KiB After Width: | Height: | Size: 144 KiB |
|
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 37 KiB |
|
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 9.9 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 249 KiB After Width: | Height: | Size: 249 KiB |
|
Before Width: | Height: | Size: 89 KiB After Width: | Height: | Size: 89 KiB |
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 6.5 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 66 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 194 KiB After Width: | Height: | Size: 194 KiB |
|
Before Width: | Height: | Size: 81 KiB After Width: | Height: | Size: 81 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 5.8 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 5 KiB After Width: | Height: | Size: 5 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 139 KiB After Width: | Height: | Size: 139 KiB |
|
Before Width: | Height: | Size: 160 KiB After Width: | Height: | Size: 160 KiB |
|
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 121 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 5.8 KiB |
|
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 81 KiB After Width: | Height: | Size: 81 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 7.4 KiB After Width: | Height: | Size: 7.4 KiB |
|
Before Width: | Height: | Size: 7.3 KiB After Width: | Height: | Size: 7.3 KiB |
|
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 146 KiB After Width: | Height: | Size: 146 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 85 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 130 KiB After Width: | Height: | Size: 130 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 425 KiB After Width: | Height: | Size: 425 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 63 KiB |
|
Before Width: | Height: | Size: 164 KiB After Width: | Height: | Size: 164 KiB |
|
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 94 KiB After Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
|
@ -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:
|
||||
|
|
|
|||
18
zk/Appending_to_files_in_Python.md
Normal 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()
|
||||
```
|
||||
24
zk/File_CRUD_operations_in_Python.md
Normal 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')
|
||||
```
|
||||
20
zk/File_system_error_handling_in_Python.md
Normal 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)
|
||||
```
|
||||
|
|
@ -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
|
|
@ -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)
|
||||
```
|
||||
32
zk/Writing_to_files_in_Python.md
Normal 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
|
|
@ -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.
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
tags: [ARPA, ARPANET, networks, computer-history]
|
||||
tags: [ARPANET, networks, computer-history]
|
||||
created: Friday, October 18, 2024
|
||||
---
|
||||
|
||||
|
|
|
|||