Understanding defer in Go.
Deferred Function Calls
A defer statement is an ordinary function or method call prefixed by the keyword defer. The function and argument expressions are evaluated when the statement is executed, but the actual call is def...
In Python, there are 2 ways to delete the contents of a folder.
Using rmtree Method
The shutil.rmtree() method deletes an entire directory tree; path must point to a directory. For example,
#!/usr/bin/python3
# Import module
import os, shutil
...
In Python, there are 3 ways to get the size of a file.
Using os.path.getsize Method
The os.path.getsize() method returns the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible. For example,
#!/usr/bin/python3
#...
In Python, there are 2 ways to get the filename without the extension from a path.
Using pathlib Module
You can use .stem from pathlib to get the filename without the extension from a path, it really simple. For example,
#!/usr/bin/python3
# Im...
In Python, there are 2 ways to extract extension from filename.
Using os.path.splitext Method
The os.path.splitext(path) method splits the pathname path into a pair (root, ext) such that root + ext == path, and the extension, ext, is empty or begins...
In Golang, there are 3 ways to read a whole file into a string variable.
Using ioutil.ReadFile Function
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads...
In Python, there are 3 ways to list all files of a directory.
Using os.listdir Method
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.' and ...
In Golang, there are 2 ways to check if a file exists.Using os.Stat Function
Stat returns a FileInfo describing the named file. See the following example:
package main
import (
"errors"
"fmt"
"os"
)
func main() {
// since the additio...
In this program, we will open a text file and read the file line by line and print the result.Using lines Method
The method lines() returns an iterator over the lines of a file. The following example:
use std::fs::File;
use std::io::{self, BufRead...
In Rust, there are two ways to install rust in Linux.Running the following in your terminal
If you’re running macOS, Linux, or another Unix-like OS. To download Rustup and install Rust, run the following in your terminal, then follow the on-screen i...
Hello, World! is the first basic program in any programming language.Python Version
Print the Python version number and exit. Example output could be:
Python version
python -V
python --version
Python 3.9.1
Running Files with Command Lines
You c...
history command is used to view the previously executed command.
The history command works with the command history list. When the command is issued with no options, it prints the history list.
The History library provides a history expansion featur...
Print the first 10 lines of each FILE to standard output.
head is a program on Unix and Unix-like operating systems used to display the beginning of a text file or piped data.
By default, head will print the first 10 lines of its input to the stand...
Compress or uncompress FILEs.
Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modi‐ficati...
Search for PATTERN in each FILE or standard input.
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search for a regular expression and p...