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