Path management

Tool
R
Python
Author

Tony D

Published

March 27, 2025

A guide to managing file paths in R and Python, with a focus on the fs and here packages in R.

This document addresses the common challenge of managing file paths across different operating systems, such as Windows and Mac. It provides a guide to using dedicated packages in R and Python to handle file paths in a robust and reproducible way. The R section focuses on the fs package for a wide range of file system operations and the here package for creating project-relative paths that work seamlessly across different environments. The Python section is a placeholder for future content.

Since windows and mac have different file path, we use package to manage the file path.

fs pacakge

Code
#pak::pkg_install('here')
#pak::pkg_install('fs')
library(fs)
Code
getwd()

list file in the current directory

Code
dir_ls()

show the current directory file info

Code
dir_info()

show the current directory file tree

Code
dir_tree()

create a new temp directory

Code
# create a new directory
tmp <- dir_create(file_temp())
tmp

create new files in that directory

Code
# create new files in that directory
file_create(path(tmp, "my-file.txt"))
dir_ls(tmp)

remove files from the directory

Code
# remove files from the directory
file_delete(path(tmp, "my-file.txt"))
dir_ls(tmp)

remove the directory

Code
# remove the directory
dir_delete(tmp)

here package

Code
library(here)

show project directory

Code
here()

show all file in the project directory

Code
list.files(here())

show one file path

here.png in images folder

Code
image_path=here('posts/path_managment/images','here.png')
image_path