All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Use Deferred Function Calls in Golang

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...
ada

How to Delete the Contents of a Folder in Python

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 ...
Tomoki

How to Get the Size of a File in Python

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 #...
aweis

How to Get the Filename Without the Extension from a Path in Python

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...
Sambhav Khandelwal

How to Extract Extension from Filename in Python

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...
Unused

How to Read a Whole File into a String Variable in Go

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...
aweis

How to List All Files of a Directory in Python

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 ...
ada

How to Check if a File Exists in Go

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...
aweis

How To Read a File Line-by-Line in Rust

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...
Patcher56

How To Install Rust in Linux

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...
pooriabt

Hello World in Python

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...
ada

How to use the history command in Linux

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...

How to use the head command in Linux

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...

How to use the gzip command in Linux

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...

How to use the grep command in Linux

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...