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

How to Get correct file base name in Golang

Golang path and filepath Examples. Using filepath.Base The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base: package main import ( "fmt" "path/filepath" ) func main() { path ...
Patcher56

How to Get Name of Current Script in Python

In Python, using the __file__ is the easiest way to get name of current script. Using Module Attributes You can use __file__ to get the name of the current file. When used in the main module, this is the name of the script that was originally invoke...
Unused

How to Get the Full URL in PHP

In PHP, using the $_SERVER variable is the easiest way to get the full URL. Using $_SERVER Variable $_SERVER is an array containing information such as headers, paths, and script locations. The following example should cover whatever you are trying...
url
Sambhav Khandelwal

How to Set the Current Working Directory in Python

In Python, using the os.chdir method is the easiest way to set the current working directory Using chdir Method The os.chdir(path) method changes the current working directory to path. For example, #!/usr/bin/python3 # Import module import os ...
Unused

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 Change the Current Directory in Go

In Golang, there are 2 ways to change the current directory. Using Dir Property Usually if you need a command to run from a specific directory, you can specify that as the Dir property on the Command, for example: package main import ( "os/...
Unused

How to Check if Directory on Path is Empty in Go

In Golang, using the Readdirnames function is the easiest way to check if directory on path is empty. Using Readdirnames Function The file.Readdirnames(n int) function reads the contents of the directory associated with file and returns a slice of ...
Sambhav Khandelwal

How to Set Environment Variables in Python

In Python, using the os.environ variable is the easiest way to set environment variables Using os.environ Variable os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set ...
Sambhav Khandelwal

How to Get the System Hostname in Python

In Python, there are 3 ways to get the system hostname. Using gethostname Method The socket.gethostname() method returns a string containing the hostname of the machine where the Python interpreter is currently executing. For example, #!/usr/bin...
Tomoki

How to Retrieve a Module's Path in Python

In Python, there are 2 ways to retrieve a module's path. Using __file__ Attribute You can retrieve a module's path using the __file__ attribute. For example, #!/usr/bin/python3 # Import module import os print(os.__file__) /usr/local/lib/pyt...
aweis

How to Get an Absolute File Path in Python

In Python, there are 2 ways to get an absolute file path. Using abspath Function The os.path.abspath() method returns a normalized absolutized version of the pathname path. For example, #!/usr/bin/python3 # Import module import os abs = os.pat...
aweis

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 List Files of a Directory in Rust

In Rust, using the read_dir function is the easiest way to list files of a directory. Using read_dir Function The fs::read_dir() function returns an iterator over the entries within a directory. See the following example: use std::fs; fn main...
ada

How to Check if a String is a Valid URL in Go

In Golang, there are 2 ways to validate URL with standard package. Using ParseRequestURI Function url.ParseRequestURI parses a raw url into a URL structure. It assumes that url was received in an HTTP request, so the url is interpreted only as an ab...
pooriabt

How to Get File Creation and Modification Dates Times in Python

In Python, there are 3 ways to get file creation and modification dates times. Using os.path Function The os.path.getmtime() function returns the time of last modification of path. The os.path.getctime() function returns the system’s ctime which, o...
Patcher56