1.3.3.9. File utilities

See also the api documentation: file

1.3.3.9.1. Introduction

The file module provides various file system related features such as:
  • filesystem traversal with depth support
  • file search, wildcard or regex based
  • file rollover (backup)
  • size parsing and formatting
  • directory creation without error on existing directory

1.3.3.9.2. File system traversal with depth support

walk()

1.3.3.9.4. Making file backups (called rollover)

See rollover()

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from shutil import rmtree
from tempfile import mkdtemp
from os.path import join

import vacumm.misc.file as F

tmpdir = mkdtemp(dir='.')

afile = join(tmpdir, 'afile')
count = 2
suffix = '.backup%d'

try:

    F.rollover(afile, count=count, suffix=suffix)
    # nothing done, afile does not exists
    with file(afile, 'w') as f: f.write('0')
    # we created afile
    # afile contains 0
    F.rollover(afile, count=count, suffix=suffix)
    # did a copy of afile to afile.backup1
    with file(afile, 'w') as f: f.write('1')
    # afile contains 1
    # afile.backup1 contains 0
    F.rollover(afile, count=count, suffix=suffix)
    # did a copy of afile.backup1 to afile.backup2, and a copy of afile to afile.backup2
    with file(afile, 'w') as f: f.write('2')
    # afile contains 2
    # afile.backup1 contains 1
    # afile.backup2 contains 0
    F.rollover(afile, count=count, suffix=suffix)
    # afile.backup2 removed, copy of afile.backup1 to afile.backup2, copy of afile to afile.backup2
    # afile contains 2
    # afile.backup1 contains 2
    # afile.backup2 contains 1

finally:
    # cleaning
    rmtree(tmpdir)
print 'Empty output'

1.3.3.9.5. Displaying and analysing file sizes

See strfsize() and strpsize()

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import vacumm.misc.file as F

sizes = (
    1 * 10**3,
    1 * 2**10,
    1 * 10**6,
    1 * 2**20,
    1 * 10**9,
    1 * 2**30,
    1 * 10**12,
    1 * 2**40,
    1 * 10**15,
    1 * 2**50,
)

for size in sizes:
    fsize = F.strfsize(size, si=False)
    fsisize = F.strfsize(size, si=True)
    print 'size: %(size)14d, formatted: %(fsize)8s (CEI, SI: %(fsisize)8s)'%vars()

/local/tmp/sraynaud/miniconda2/lib/python2.7/site-packages/cmocean/tools.py:76: MatplotlibDeprecationWarning: The is_string_like function was deprecated in version 2.1.
  if not mpl.cbook.is_string_like(rgbin[0]):
size:           1000, formatted:  1000 io (CEI, SI:     1 Ko)
size:           1024, formatted:    1 Kio (CEI, SI: 1.024 Ko)
size:        1000000, formatted: 976.562 Kio (CEI, SI:     1 Mo)
size:        1048576, formatted:    1 Mio (CEI, SI: 1.049 Mo)
size:     1000000000, formatted: 953.674 Mio (CEI, SI:     1 Go)
size:     1073741824, formatted:    1 Gio (CEI, SI: 1.074 Go)
size:  1000000000000, formatted: 931.323 Gio (CEI, SI:     1 To)
size:  1099511627776, formatted:    1 Tio (CEI, SI: 1.100 To)
size: 1000000000000000, formatted: 909.495 Tio (CEI, SI:     1 Po)
size: 1125899906842624, formatted:    1 Pio (CEI, SI: 1.126 Po)

1.3.3.9.6. Creating directories

See mkdirs() and mkfdirs()

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from os.path import abspath, dirname, join, realpath
from shutil import rmtree
from tempfile import mkdtemp

import vacumm.misc.file as F

tmpdir = mkdtemp(dir='.')

adir = join(tmpdir, 'path/to/adir')
afile = join(adir, 'afile')

anotherdir = join(tmpdir, 'path/to/anotherdir')
anotherfile = join(anotherdir, 'anotherfile')

try:
    
    print 'Created directory:', F.mkdirs(adir)
    F.mkfdirs(afile) # no effect, already created
    print 'Created directories:',  F.mkfdirs((afile, anotherfile))
    F.mkdirs((adir, anotherdir)) # no effect, already created
    
finally:
    rmtree(tmpdir)

/local/tmp/sraynaud/miniconda2/lib/python2.7/site-packages/cmocean/tools.py:76: MatplotlibDeprecationWarning: The is_string_like function was deprecated in version 2.1.
  if not mpl.cbook.is_string_like(rgbin[0]):
Created directory: ./tmp7W0Awt/path/to/adir
Created directories: ['./tmp7W0Awt/path/to/anotherdir']