LogoLogo
Official SiteAPI Reference
stable/3.0
stable/3.0
  • Cement Developer Guide
  • Release Information
    • What's New!
    • Upgrading
    • ChangeLog
    • Deprecations
  • Getting Started
    • Installation
    • Developer Tools
    • Framework Overview
    • Beginner Tutorial
      • Part 1: Creating Your First Project
      • Part 2: Adding Features
      • Part 3: Extending a Project
      • Part 4: Making Everything Legit
  • Core Foundation
    • Interfaces and Handlers
    • Hooks
    • Configuration Settings
    • Arguments
    • Logging
    • Controllers
    • Output Rendering
    • Caching
    • Mail Messaging
    • Framework Extensions
    • Application Plugins
    • Templating
  • Utilities
    • Filesystem
    • Shell
    • Miscellaneous
  • Extensions
    • Alarm
    • Argparse
    • Colorlog
    • ConfigParser
    • Daemon
    • Dummy
    • Generate
    • Jinja2
    • Json
    • Logging
    • Memcached
    • Mustache
    • Plugin
    • Print
    • Redis
    • Scrub
    • SMTP
    • Tabulate
    • Yaml
    • Watchdog
  • Additional Topics
    • Extending The App Object
    • Unit Testing
    • Cleanup
    • Signal Handling
    • Pipenv
    • Autocomplete
    • Profiling with cProfile
    • Debugging with VSCode
  • Environment Variables
  • Terminology
  • Contributing
  • Privacy Policy
Powered by GitBook
On this page
  • Introduction to the Filesystem Utilities
  • Temporary Directories and Files
  • Backup Directories and Files
  • Filesystem Paths
  1. Utilities

Filesystem

PreviousUtilitiesNextShell

Last updated 6 years ago

Introduction to the Filesystem Utilities

Cement includes a with helpers for common tasks related to filesystem management.

API References:

Temporary Directories and Files

Creating and cleaning up temporary directories and files can be tedious, so we created the class to make management easy:

example.py
import os
from cement import fs

# create a tmp object (includes dir and file)
with fs.Tmp() as t:
    # do something with a temporary dir
    print('Temp Dir: %s' % t.dir)
    os.listdir(t.dir)

    # do something with a temporary file
    print('Temp File: %s' % t.file)
    with open(t.file, 'w') as f:
        f.write('some data')
$ python example.py
Temp Dir: /var/folders/jm/cr24ncsn1lgdblxm2mvgtdt40000gn/T/tmpwzo8kh89
Temp File: /var/folders/jm/cr24ncsn1lgdblxm2mvgtdt40000gn/T/tmps4s7n8xg

When using the Python with operator, the temporary directory and file are automatically cleaned up when exiting the block.

Backup Directories and Files

from cement import fs

# create a backup of a file or directory
fs.backup('/path/to/my/file')

Filesystem Paths

Best practice when working with paths is to use os.path.join() to ensure cross-platform compatibility and also expanding the absolute path to account for things like ~ (user home dir) on Linux. We've created a number of helpers to meld these together for common tasks:

from cement import fs

# expand the full absolute path
fs.abspath('./some/relative/path')
fs.abspath('~/some/user/path/')

# join a path while also expanding absolute path
fs.join('~', '.myapp')
fs.join('.', 'some', 'sub-dir')

# join a path and test that it exists
path,exists = fs.join_exists('~', '.myapp')
if exists is True:
    # do something with the full absolute path
    os.listdir(path)

# ensure a directory exists
fs.ensure_dir_exists('/path/to/some/dir')

# ensure a parent directory exists
fs.ensure_parent_dir_exists('/path/to/parent/child')

Creating a .bak of a directory or file before modifying it is one of the most common tasks when working with the filesystem, but often takes some tedious code to do so without overwriting existing backups. The creates a .bak file, or if it exists .bak.0, .bak.1, .bak.2, etc.

Filesystem Utility Module
Cement Filesystem Utility Module
fs.Tmp
fs.backup()