Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Python – File(read/write)

A file is a contiguous set of bytes used to store data. This data is organized in a specific format and can be anything as simple as a text file or as complicated as a program executable. These byte files are then translated into binary 1 and 0 for easier processing by the computer.

A file has 3 main parts: 

  • Header – metadata about the content of the file(file name, size, type, etc)
  • Data – content of the file
  • End of file(EOF) – special character as indicator for end of file

What this data represents depends on the format specification used, which is typically represented by an extension. For example, a file that has an extension of .txt most likely conforms to the text file specification. There are hundreds, if not thousands, of  file extensions out there.

In Python, a file operation takes place in the following order:

  1. Open a file
  2. Read or write (perform operation)
  3. Close the file

We use the open function to read and write to files.

Syntax

variable = open(filename, mode)

## mode ##
"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exist

In addition you can specify if the file should be handled as binary or text mode 
"t" - Text - Default value. Text mode 
"b" - Binary - Binary mode (e.g. images)

 

Writing to file

testFile = open("test.txt", "w")

# write strings to file

about_me = str("I am a developer.\nI am nice.\nI am tall.")

testFile.write(about_me)

testFile.close()

Using with

The with statement automatically takes care of closing the file once it leaves the with block, even in cases of error. I highly recommend that you use the with statement as much as possible, as it allows for cleaner code and makes handling any unexpected errors easier for you.

with open("test.txt", "a") as test2File:
    # write strings to file
    about_me = str("I am a developer2.\nI am nice2.\nI am tall2.")

    test2File.write(about_me)

 

Reading a file

print("reading test.txt file...")
with open("test.txt", "r") as reader:
    # read all content
    #print(reader.read())

    # read line by line
    print("read line by line")
    lines = reader.readlines()
    for line in lines:
        print(line)

Note that you should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.

Character encoding

Another common problem that you may face is the encoding of the byte data. An encoding is a translation from byte data to human readable characters. This is typically done by assigning a numerical value to represent a character. The two most common encodings are the ASCII and UNICODE Formats. ASCII can only store 128 characters, while Unicode can contain up to 1,114,112 characters.

ASCII is actually a subset of Unicode (UTF-8), meaning that ASCII and Unicode share the same numerical to character values. It’s important to note that parsing a file with the incorrect character encoding can lead to failures or misrepresentation of the character. For example, if a file was created using the UTF-8 encoding, and you try to parse it using the ASCII encoding, if there is a character that is outside of those 128 values, then an error will be thrown.

Source code on Github

March 6, 2020

CSS Shadows

Text Shadow

text-shadow property applies shadow to text.

<div class="row">
    <div class="col-4">
        <h4>Text Shadow</h4>
    </div>
    <div class="col-8" id="textShadow">
        Hi my name is Folau
    </div>
</div>
<style>
            #textShadow{
                /* 
                text-shadow: h-shadow v-shadow blur-radius color 

                h-shadow - The position of the horizontal shadow
                v-shadow - The position of the vertical shadow.
                blur-radius - The blur radius. Default value is 0
                color - color
                */
                text-shadow: 2px 2px 8px #FF0000;
            }
</style>

Text Multiple Shadows

<div class="row">
    <div class="col-4">
        <h4>Text Multiple Shadow</h4>
    </div>
    <div class="col-8" id="textMultipleShadow">
        Hi my name is Folau
    </div>
</div>
<style>
            #textMultipleShadow{
                color: white;
                text-shadow: 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;
            }
</style>

 

Box Shadow

box-shadow property applies shadow to elements.

<div class="row">
    <div class="col-4">
        <h4>Box Shadow</h4>
    </div>
    <div class="col-8" id="boxShadow">
        Hi my name is Folau
    </div>
</div>
<style>
            #boxShadow{
                /*
                box-shadow: none(no shadow)
                box-shadow: h-offset v-offset blur spread color

                h-offset - The horizontal offset of the shadow. A positive value puts the shadow on the right side of the box, a negative value puts the shadow on the left side of the box
                v-offset - The vertical offset of the shadow. A positive value puts the shadow below the box, a negative value puts the shadow above the box
                blur - The blur radius. The higher the number, the more blurred the shadow will be
                spread - The spread radius. A positive value increases the size of the shadow, a negative value decreases the size of the shadow
                color - color
                */
                box-shadow: 10px 10px 8px 10px #888888;
            }
</style>

Box Multiple Shadows

<div class="row">
    <div class="col-4">
        <h4>Box Multiple Shadow</h4>
    </div>
    <div class="col-8" id="boxMultipleShadow">
        <img src="superman.jpeg" alt=""  style="width:100%;padding: 10px 30px;"/>
        <div class="profile">
            Hi my name is Folau
        </div>
    </div>
</div>
<style>
    #boxMultipleShadow{
                box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
                text-align: center;
            }
        </style>

 

Source code on Github

March 6, 2020

Linux Zip

Zip

Zip is a compression tool. Zip files have the .zip extension. zip is very useful when you are on a limited bandwidth and need to send a big file over the internet.

zip {options} {zipFilename} {files…}

zip zipfile.zip test.txt test1.txt

unzip {zipFilename}

unzip myfile.zip

 

Gzip

gzip command compresses files. Each single file is compressed into a single file. The compressed file consists of a GNU zip header and deflated data.

If given a file as an argument, gzip compresses the file, adds a “.gz” suffix, and deletes the original file. With no arguments, gzip compresses the standard input and writes the compressed file to standard output.

 

gzip {options} {files…}

gzip test.txt test1.txt

gunzip {gzipFile}

gunzip test.gz

 

 

 

 

March 6, 2020

Linux Vi Editor

vi stands for visual editor and comes as one of default applications in every Linux/Unix system. The vi editor has two modes.

  • Command Mode: In command mode, actions are taken on the file. The vi editor starts in command mode. Here, the typed words will act as commands in vi editor. To pass a command, you need to be in command mode.
  • Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take you to the command mode from insert mode.

If you are not sure which mode you are in, press Esc key twice and you’ll be in command mode.

 

Create a file with vi {filename}

vi test.txt
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     
~                                                                                                                                                                                                     

 

Save file

// save and quit
:wq

// save
:w

// quit
:q

// save as filename
:w fname

// save and quit
ZZ

// quit and disregard work
:q!

// quit and save qork
:wq!

 

How to delete on command mode

// delete current character
x

// Replace the current character
r

// Switch two characters
xp

// Delete the current line
dd

// Delete the current line from current character to the end of the line
D

// delete from the current line to the end of the file
dG

 

Repeat and undo

// Undo the last command
u

// repeat the last command
.

 

Copy, Cut, and Paste

// Delete a line
dd

// (yank yank) copy a line
yy

// Paste after the current line
P

// Paste before the current line
p

// Delete the specified n number of lines
{n}dd

// Copy the specified n number of lines
{n}yy


 

Search string

// Forward search for given string
/{string}

// Backward search for given string
?{string}

// Forward search string at beginning of a line
/^{string}

// Forward search string at end of a line
/{string}$

// Go to next occurrence of searched string
n

// Search for the word he (and not for there, here, etc.)
/\<he\>

// Search for place, plbce, and plcce
/pl[abc]ce

 

Replace all

:{startLine,endLine} s/{oldString}/{newString}/g

// Replace forward with backward from first line to the last line
:1,$ s/readable/changed/

 

  

 

 

March 6, 2020

Linux System Admin

A system administrator manages configuration, upkeep and reliable operations of computer operations. Sysadmin handles servers, has to manage system performance and security without exceeding the budget to meet users need.

A system administrator only deals with terminal interface and hence it is very important to learn and become master in commands to operate from terminal.

Uptime

The uptime command tells us how long a system has been running.

folaukaveinga@Folaus-MacBook-Pro-3 sidecar-api-partner % uptime
18:34  up 19:47, 4 users, load averages: 3.05 2.49 2.30

From the left, it shows,

  • currrent system time
  • duration for which system has been running (system is running since 18 minutes)
  • number of users logged in (2 users are logged in)
  • system load average CPU load for past 1, 5 and 15 minutes.

 

WGET

wget is used to download files onto the current directory.

wget {url}

folaukaveinga@Folaus-MacBook-Pro-3 Downloads % wget google.com  
--2020-11-09 21:31:28--  http://google.com/
Resolving google.com (google.com)... 2607:f8b0:400f:805::200e, 172.217.11.238
Connecting to google.com (google.com)|2607:f8b0:400f:805::200e|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.google.com/ [following]
--2020-11-09 21:31:28--  http://www.google.com/
Resolving www.google.com (www.google.com)... 172.217.2.4, 2607:f8b0:400f:800::2004
Connecting to www.google.com (www.google.com)|172.217.2.4|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.1’

index.html.1                                          [ <=>                                                                                                        ]  12.62K  --.-KB/s    in 0s      

2020-11-09 21:31:30 (88.6 MB/s) - ‘index.html.1’ saved [12918]

 

wget -o {filename} {url}

 To save file with a different name option O can be used.

wget -o google.html google.com

Download a whole website for local use

wget --mirror -p --convert-links -P {path} {url}

// example. 
wget --mirror -p --convert-links -P ./yourtimer https://yourtimer.io

–mirror – it enables the options suitable for mirroring

-p – download all files which are necessary to display html page

–convert-links – after downloading, convert links into documents for local viewing

-P ./ local dir ? save whole website in the specified local directory.

 

wget –tries={number} {url}

By default, wget command tries 20 times to downlod a file. This problem generally come when a large file is getting downloaded and internet connection is weak.

Here, you can set number of attempts which wget should make to download a file.

wget -O yourtimer.html --tries=10 https://yourtimer.io

 

SFTP

SFTP to a server with ssh key.

sftp -o IdentityFile={ssh-private-key} user@host

 

folaukaveinga@Folaus-MacBook-Pro-3 ~ % sftp -o IdentityFile=~/.ssh/testa/server_ssh_key ubuntu@qa.testa.com 
Connected to qa.testa.com.
sftp> ls
testa.sql
sftp>

// use quit or ! to exit

 

sftp upload

PUT {filename}

sftp> PUT test.txt

// upload multiple tiles
sftp> MPUT *.txt

 

 

sftp download

GET -p  {remoteFile} {localFilename}

sftp> GET -p bin-deploy.sh bin.sh

// download multiple files
sftp> MGET -p *.txt

 

 

March 6, 2020