Examples on file operations

Directories

  • Getting current working directory
  • Defining temporary files and temporary folders
  • Getting the directory and the basename of files
from rlike import *
getwd()
'/home/runner/work/rlike/rlike/docs'
x = tempdir()
x
'/tmp/tmp4dldswsq'
x = tempfile(pattern = 'hello', fileext = '.txt')
x
'/tmp/tmpzk38vobp/helloq93lcfea.txt'
dirname(x)
'/tmp/tmpzk38vobp'
basename(x)
'helloq93lcfea.txt'

Listing / paths / extensions

  • Listing files in a directory
  • Constructing paths to files
  • Check if files exist
from rlike import *
x = list_files(pattern = ".txt", recursive = True)
x = list_files(recursive = False)
file_exists(x)
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
x = file_path(getwd(), "subfolder", ["file1.txt", "file2.txt"])
file_ext(x)
['.txt', '.txt']
file_path_sans_ext(x)
['/home/runner/work/rlike/rlike/docs/subfolder/file1', '/home/runner/work/rlike/rlike/docs/subfolder/file2']

Removing / copying

  • Removing files and directories
folder = tempdir()
x = tempfile(pattern = 'hello_x', fileext = '.txt', tmpdir = folder)
writeLines(text = "hi", con = x)
'/tmp/tmp2yyue8l8/hello_x67np7itg.txt'
y = tempfile(pattern = 'hello_y', fileext = '.txt', tmpdir = folder)
writeLines(text = "hi again", con = y)
'/tmp/tmp2yyue8l8/hello_ytamcheub.txt'
file_exists([x, y])
[True, True]
file_copy(x, y)
'/tmp/tmp2yyue8l8/hello_ytamcheub.txt'
readLines(y)
['hi']
file_remove([x, y])
[None, None]
unlink(folder)
  • Location of installed python module
x = system_file(package = 'rlike') 
x
'/home/runner/.virtualenvs/r-reticulate/lib/python3.12/site-packages/rlike'