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 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 Get a List of All Subdirectories in the Current Directory in Python

In Python, there are 2 ways to get a list of all subdirectories in the current directory. Using os.walk Function The os.walk() method generates the file names in a directory tree by walking the tree either top-down or bottom-up. The following examp...
pooriabt

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

How to Move a File in Python

In Python, there are 3 ways to move a file. Using os Module The os.rename() method renames the file or directory src to dst. If dst exists, the operation will fail with an OSError subclass in a number of cases: #!/usr/bin/python3 # Import modu...
aweis

How to Check If a Directory Exists in Python

In Python, there are 3 ways to check if a directory exists. Using os.path.isdir Method You can use os.path.isdir() to check if a directory exists, it really simple. For example, #!/usr/bin/python3 # Import module import os d = "./folder" print...
aweis

How to Check If an Object Is of a Given Type in Python

In Python, there are 2 ways to check if an object is of a given type. Using isinstance Method The isinstance(object, classinfo) method returns True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtua...
Unused

How to Create Nested Directories in Go

In Golang, using the os.MkdirAll function is the easiest way to create nested directories Using os.MkdirAll Function The os.MkdirAll() function creates a directory named path, along with any necessary parents, and returns nil, or else returns an er...
Sambhav Khandelwal

How to Get the Directory of the Currently Running File in Go

In Golang, there are 3 ways to get the directory of the currently running file.Using os.Executable Function Executable returns the path name for the executable that started the current process. The easiest way to get the directory of the currently ...
Patcher56

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