pathlib is an R package that provides a comprehensive set of
functions for performing filesystem path operations, inspired by
Python’s pathlib
module. This package aims to make path manipulations
in R intuitive and accessible, especially for users familiar with
Python’s pathlib
methods. The functions in pathlib leverage the
fs
and checkmate
packages to ensure efficient and reliable path
operations.
You can install the pathlib
package directly from GitHub.
# Install devtools if you haven't already
install.packages("devtools")
# Install pathlib package from GitHub
devtools::install_github("pythonicr/pathlib")
Here are some examples demonstrating how to use the functions provided by the pathlib package.
The Path_absolute
function returns the absolute path of the input
path, similar to Python’s pathlib.Path.resolve()
or
pathlib.Path.absolute()
methods.
library(pathlib)
# Get the absolute path
abs_path <- Path_absolute("relative/path/to/file.txt")
print(abs_path)
The Path_chmod
function changes the mode (permissions) of the
specified file or directory.
# Change file permissions
Path_chmod("file.txt", "755")
The Path_cwd
function returns the current working directory, similar
to Python’s pathlib.Path.cwd()
method.
# Get the current working directory
current_dir <- Path_cwd()
print(current_dir)
The Path_exists
function checks if the specified files or directories
exist.
# Check if paths exist
exists <- Path_exists("file.txt")
print(exists)
The Path_expanduser
function replaces the tilde (~) character with the
user’s home directory path.
# Expand tilde in paths
expanded_path <- Path_expanduser("~/Documents/file.txt")
print(expanded_path)
The Path_glob
function searches for files matching the specified
patterns and returns their paths.
# Glob for files
files <- Path_glob("*.txt")
print(files)
The Path_group
function returns the group owner of the provided paths.
# Get group owner of files
group_owner <- Path_group("file.txt")
print(group_owner)
The Path_hardlink_to
function creates hard links from the specified
paths to the target paths.
# Create hard link
Path_hardlink_to("file.txt", "hardlink.txt")
The Path_home
function returns the user’s home directory.
# Get home directory
home_dir <- Path_home()
print(home_dir)
The Path_is_absolute
function checks if the specified paths are
absolute paths.
# Check if paths are absolute
is_abs <- Path_is_absolute("/path/to/file.txt")
print(is_abs)
The Path_is_block_device
function checks if the specified paths are
block devices.
# Check if paths are block devices
is_block_device <- Path_is_block_device("file.txt")
print(is_block_device)
The Path_is_char_device
function checks if the specified paths are
character devices.
# Check if paths are character devices
is_char_device <- Path_is_char_device("file.txt")
print(is_char_device)
The Path_is_dir
function checks if the specified paths are
directories.
# Check if paths are directories
is_dir <- Path_is_dir("directory")
print(is_dir)
The Path_is_fifo
function checks if the specified paths are FIFOs
(named pipes).
# Check if paths are FIFOs
is_fifo <- Path_is_fifo("fifo_pipe")
print(is_fifo)
The Path_is_file
function checks if the specified paths are regular
files.
# Check if paths are files
is_file <- Path_is_file("file.txt")
print(is_file)
The Path_is_socket
function checks if the specified paths are sockets.
# Check if paths are sockets
is_socket <- Path_is_socket("socket_file")
print(is_socket)
The Path_is_symlink
function checks if the specified paths are
symbolic links.
# Check if paths are symbolic links
is_symlink <- Path_is_symlink("symbolic_link")
print(is_symlink)
The Path_lstat
function retrieves file information for the provided
paths without following symbolic links.
# Get file information without following symbolic links
file_info <- Path_lstat("file.txt")
print(file_info)
The Path_mkdir
function creates directories at the specified paths. If
the parents
argument is set to TRUE, it creates parent directories as
needed.
# Create directory
Path_mkdir("new_directory")
The Path_owner
function returns the owner of the provided paths.
# Get owner of files
owner <- Path_owner("file.txt")
print(owner)
The Path_read_text
function reads the content of a file at the
specified path as text.
# Read text from file
text_content <- Path_read_text("file.txt", encoding = "UTF-8")
print(text_content)
The Path_readlink
function returns the target path of the specified
symbolic links.
# Get target path of symbolic link
target_path <- Path_readlink("symbolic_link")
print(target_path)
The Path_rename
function renames files or directories from their
current paths to new paths.
# Rename file
Path_rename("old_file.txt", "new_file.txt")
The Path_rmdir
function removes directories at the specified paths.
# Remove directory
Path_rmdir("empty_directory")
The Path_stat
function retrieves file information for the provided
paths, allowing an optional argument follow_symlinks
to specify
whether symbolic links should be followed when obtaining file
information.
# Get file information
file_info <- Path_stat("file.txt", follow_symlinks = TRUE)
print(file_info)
The Path_symlink_to
function creates symbolic links from the specified
paths to the target paths.
# Create symbolic link
Path_symlink_to("source_file.txt", "target_file.txt")
The Path_touch
function creates the files if they do not exist and
updates the access and modification times if they do exist.
# Touch file
Path_touch("file.txt")
The Path_unlink
function unlinks (deletes) files at the specified
paths.
# Unlink file
Path_unlink("file.txt")
The Path_write_text
function writes the provided text to a file at the
specified path.
# Write text to file
Path_write_text("file.txt", "Hello, World!")
We welcome contributions to the pathlib package. If you have suggestions, bug reports, or want to contribute code, please open an issue or submit a pull request on our GitHub repository.
- pathlib.Path.absolute
- pathlib.Path.chmod
- pathlib.Path.cwd
- pathlib.Path.exists
- pathlib.Path.expanduser
- pathlib.Path.glob
- pathlib.Path.group
- pathlib.Path.hardlink_to
- pathlib.Path.home
- pathlib.Path.is_block_device
- pathlib.Path.is_char_device
- pathlib.Path.is_dir
- pathlib.Path.is_fifo
- pathlib.Path.is_file
- pathlib.Path.is_junction
- pathlib.Path.is_mount
- pathlib.Path.is_socket
- pathlib.Path.is_symlink
- pathlib.Path.iterdir
- pathlib.Path.lchmod
- pathlib.Path.lstat
- pathlib.Path.mkdir
- pathlib.Path.open
- pathlib.Path.owner
- pathlib.Path.read_bytes
- pathlib.Path.read_text
- pathlib.Path.readlink
- pathlib.Path.rename
- pathlib.Path.replace
- pathlib.Path.resolve
- pathlib.Path.rglob
- pathlib.Path.rmdir
- pathlib.Path.samefile
- pathlib.Path.stat
- pathlib.Path.symlink_to
- pathlib.Path.touch
- pathlib.Path.unlink
- pathlib.Path.walk
- pathlib.Path.write_bytes
- pathlib.Path.write_text
pathlib is released under the MIT License. See the LICENSE file in the package’s repository for more details.