Autosave: 2023-03-08 07:11:40
This commit is contained in:
parent
817719225c
commit
c9bdffc204
3 changed files with 45 additions and 10 deletions
|
@ -119,7 +119,7 @@ file.close()
|
|||
|
||||
### Error handling
|
||||
|
||||
Obviously file access can raise errors - typically when the file you want to access does not exist. We can manage this scenario with [exception handlers](/Programming_Languages/Python/Syntax/Error_handling_in_Python.md):
|
||||
Obviously file access can raise errors - typically when the file you want to access does not exist (i.e. a `FileNotFoundError` [exception](/Programming_Languages/Python/Syntax/Error_handling_in_Python.md)). We can manage this scenario with [exception handlers](/Programming_Languages/Python/Syntax/Error_handling_in_Python.md):
|
||||
|
||||
```py
|
||||
try:
|
||||
|
@ -143,3 +143,38 @@ 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 hace 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')
|
||||
```
|
|
@ -7,7 +7,7 @@ tags:
|
|||
|
||||
# read
|
||||
|
||||
The primary use of `read` is to capture user input from `stdin`. It is also often used frequently to parse strings or files that are redirected to it (with `<` and `<<`) or piped to it. In each case, what is read is stored as a variable.
|
||||
The primary use of `read` is to capture user input from `stdin`. It can also be used to parse strings or files that are redirected to it (with `<` and `<<`) or piped to it. In each case, what is read is stored as a variable.
|
||||
|
||||
`read` will parse line by line using a space (`\n`) as the default delimiter. You can use IFS to parse by other characters and/or [split the contents into an array](/Programming_Languages/Shell/Split_into_array.md).
|
||||
|
||||
|
@ -22,9 +22,9 @@ $ bishop
|
|||
|
||||
> If you don't specify variables, `read` will automatically parse using whitespace
|
||||
|
||||
## Example of piping to `read`
|
||||
## Example of piping to read
|
||||
|
||||
This reads the files in a directory and passes the file names to `read`.
|
||||
Here we use [find](/Programming_Languages/Shell/Find.md) to collate the files in the current directory and then pipe them to read.
|
||||
|
||||
```bash
|
||||
find -type -f -not -path "./.git/" | read $fname
|
||||
|
@ -35,8 +35,8 @@ find -type -f -not -path "./.git/" | read $fname
|
|||
We will typically read from a source and then do something with each variable that `read` returns, e.g:
|
||||
|
||||
```bash
|
||||
while read var; do
|
||||
if [var == 'something']; then
|
||||
while read line; do
|
||||
if [ var == 'something' ]; then
|
||||
# do something
|
||||
done < './input-file.txt
|
||||
```
|
||||
|
|
|
@ -32,7 +32,7 @@ echo "${name_for_array[1]}"
|
|||
456
|
||||
```
|
||||
|
||||
If we want to read direct from string within bash file:
|
||||
If we want to read a string directly:
|
||||
|
||||
```bash
|
||||
readarray -t new_name_for_array <<< "here
|
||||
|
@ -46,17 +46,17 @@ is
|
|||
|
||||
> The _-t_ flag removes the trailing newline
|
||||
|
||||
Add more: https://linuxhint.com/split-string-array-bash/
|
||||
See more: https://linuxhint.com/split-string-array-bash/
|
||||
|
||||
## read
|
||||
|
||||
For different delimiters we have to use `read`, combined with `IFS` the **Internal Field Separator**.
|
||||
For delimiters other than a space we have to use `read`, combined with `IFS` the **Internal Field Separator**.
|
||||
|
||||
For example, to split by comma:
|
||||
|
||||
```plaintext
|
||||
# comma-input.txt
|
||||
something, something else, something more
|
||||
something,something else,something more
|
||||
```
|
||||
|
||||
```bash
|
||||
|
|
Loading…
Add table
Reference in a new issue