Skip to contents

Overview

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.

Installation

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

Getting Started

Basic Usage

Here are some examples demonstrating how to use the functions provided by the pathlib package.

Get the Absolute Path

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)

Change the Mode (Permissions) of a File or Directory

The Path_chmod function changes the mode (permissions) of the specified file or directory.

# Change file permissions
Path_chmod("file.txt", "755")

Get the Current Working Directory

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)

Check if Paths Exist

The Path_exists function checks if the specified files or directories exist.

# Check if paths exist
exists <- Path_exists("file.txt")
print(exists)

Expand the Tilde (~) in File Paths

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)

Glob for Files Matching a Pattern

The Path_glob function searches for files matching the specified patterns and returns their paths.

# Glob for files
files <- Path_glob("*.txt")
print(files)

Get the Group Owner of Files or Directories

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

Get the User’s Home Directory

The Path_home function returns the user’s home directory.

# Get home directory
home_dir <- Path_home()
print(home_dir)

Check if Paths are Absolute

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)

Check if Paths are Block Devices

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)

Check if Paths are Character Devices

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)

Check if Paths are Directories

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)

Check if Paths are FIFOs (Named Pipes)

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)

Check if Paths are Regular Files

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)

Check if Paths are Sockets

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)

Create Directories

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

Get the Owner of Files or Directories

The Path_owner function returns the owner of the provided paths.

# Get owner of files
owner <- Path_owner("file.txt")
print(owner)

Read the Content of a File as Text

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)

Rename Files or Directories

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

Remove Directories

The Path_rmdir function removes directories at the specified paths.

# Remove directory
Path_rmdir("empty_directory")

Get File Information

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

Touch Files

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

Write Text to a File

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!")

License

pathlib is released under the MIT License. See the LICENSE file in the package’s repository for more details.