From c9bdffc204fa83f5d93111b2a32a6d6fbf1fb3ee Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Wed, 8 Mar 2023 07:11:40 +0000 Subject: [PATCH] Autosave: 2023-03-08 07:11:40 --- .../{Libraries => Modules}/IO_in_Python.md | 37 ++++++++++++++++++- Programming_Languages/Shell/Read.md | 10 ++--- .../Shell/Split_into_array.md | 8 ++-- 3 files changed, 45 insertions(+), 10 deletions(-) rename Programming_Languages/Python/{Libraries => Modules}/IO_in_Python.md (80%) diff --git a/Programming_Languages/Python/Libraries/IO_in_Python.md b/Programming_Languages/Python/Modules/IO_in_Python.md similarity index 80% rename from Programming_Languages/Python/Libraries/IO_in_Python.md rename to Programming_Languages/Python/Modules/IO_in_Python.md index 610324c..4df8b0a 100644 --- a/Programming_Languages/Python/Libraries/IO_in_Python.md +++ b/Programming_Languages/Python/Modules/IO_in_Python.md @@ -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') +``` diff --git a/Programming_Languages/Shell/Read.md b/Programming_Languages/Shell/Read.md index 3b74b89..05f9615 100644 --- a/Programming_Languages/Shell/Read.md +++ b/Programming_Languages/Shell/Read.md @@ -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 ``` diff --git a/Programming_Languages/Shell/Split_into_array.md b/Programming_Languages/Shell/Split_into_array.md index 8239aec..35aa881 100644 --- a/Programming_Languages/Shell/Split_into_array.md +++ b/Programming_Languages/Shell/Split_into_array.md @@ -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