The shutil module include high-level file operations.

Copying files


In [2]:
import glob
import shutil
print('Before: ', glob.glob('shutil.*'))
shutil.copyfile('shutil.ipynb', 'shutil.copy.ipynb')
print('After:' , glob.glob('shutil.*'))


Before:  ['shutil.ipynb']
After: ['shutil.copy.ipynb', 'shutil.ipynb']

In [4]:
import glob
import os
import shutil

os.mkdir('examples')
print('Before:', glob.glob('examples/*'))
shutil.copy('shutil.ipynb', 'examples')
print('After:', glob.glob('examples/*'))


Before: []
After: ['examples/shutil.ipynb']

In [9]:
import os
import shutil
import time
def show_file_info(filename):
    stat_info = os.stat(filename)
    print(' mode ', oct(stat_info.st_mode))
    print(' created: ', time.ctime(stat_info.st_ctime))
    print(' accessed: ', time.ctime(stat_info.st_atime))
    print(' Modified: ', time.ctime(stat_info.st_mtime))
os.mkdir('examples')
print('Source:')
show_file_info('shutil.ipynb')
shutil.copy2('shutil.ipynb', 'examples')
print('Dest:')
show_file_info('examples/shutil.ipynb')


Source:
 mode  0o100644
 created:  Mon Sep  4 19:14:05 2017
 accessed:  Mon Sep  4 19:14:05 2017
 Modified:  Mon Sep  4 19:14:05 2017
Dest:
 mode  0o100644
 created:  Mon Sep  4 19:14:05 2017
 accessed:  Mon Sep  4 19:14:05 2017
 Modified:  Mon Sep  4 19:14:05 2017

Copying File Metadata


In [10]:
import os
import shutil
import subprocess
with open('file_to_change.txt', 'wt') as f:
    f.write('content')
os.chmod('file_to_change.txt', 0o444)
print('Before:', oct(os.stat('file_to_change.txt').st_mode))
shutil.copymode('shutil.ipynb', 'file_to_change.txt')
print('After:', oct(os.stat('file_to_change.txt').st_mode))


Before: 0o100444
After: 0o100644

Working with Dicretionary Trees


In [11]:
import glob
import pprint
import shutil
print('Before: ')
pprint.pprint(glob.glob('/tmp/example/*'))

shutil.copytree('../FileSystem', '/tmp/example')

print('After: ')
pprint.pprint(glob.glob('/tmp/example/*'))


Before: 
[]
After: 
['/tmp/example/example',
 '/tmp/example/examples',
 '/tmp/example/file_to_change.txt',
 '/tmp/example/filecomp.ipynb',
 '/tmp/example/fnmatch',
 '/tmp/example/glob.ipynb',
 '/tmp/example/io.ipynb',
 '/tmp/example/lorem.txt',
 '/tmp/example/lorem_copy.txt',
 '/tmp/example/mmap.ipynb',
 '/tmp/example/Path.ipynb',
 '/tmp/example/shutil.copy.ipynb',
 '/tmp/example/shutil.ipynb',
 '/tmp/example/tempfile.ipynb']

In [14]:
import glob
import pprint
import shutil


def verbose_copy(src, dst):
    print('copying\n {!r}\n to {!r}'.format(src, dst))
    return shutil.copy2(src, dst)


print('BEFORE:')
pprint.pprint(glob.glob('/tmp/example/*'))
print()

shutil.copytree(
    '../FileSystem', '/tmp/example',
    copy_function=verbose_copy,
    ignore=shutil.ignore_patterns('*.py'),
)

print('\nAFTER:')
pprint.pprint(glob.glob('/tmp/example/*'))


BEFORE:
[]

copying
 '../FileSystem/.ipynb_checkpoints/filecomp-checkpoint.ipynb'
 to '/tmp/example/.ipynb_checkpoints/filecomp-checkpoint.ipynb'
copying
 '../FileSystem/.ipynb_checkpoints/glob-checkpoint.ipynb'
 to '/tmp/example/.ipynb_checkpoints/glob-checkpoint.ipynb'
copying
 '../FileSystem/.ipynb_checkpoints/io-checkpoint.ipynb'
 to '/tmp/example/.ipynb_checkpoints/io-checkpoint.ipynb'
copying
 '../FileSystem/.ipynb_checkpoints/mmap-checkpoint.ipynb'
 to '/tmp/example/.ipynb_checkpoints/mmap-checkpoint.ipynb'
copying
 '../FileSystem/.ipynb_checkpoints/Path-checkpoint.ipynb'
 to '/tmp/example/.ipynb_checkpoints/Path-checkpoint.ipynb'
copying
 '../FileSystem/.ipynb_checkpoints/shutil-checkpoint.ipynb'
 to '/tmp/example/.ipynb_checkpoints/shutil-checkpoint.ipynb'
copying
 '../FileSystem/.ipynb_checkpoints/tempfile-checkpoint.ipynb'
 to '/tmp/example/.ipynb_checkpoints/tempfile-checkpoint.ipynb'
copying
 '../FileSystem/example/dir1/common_dir/dir1/common_file'
 to '/tmp/example/example/dir1/common_dir/dir1/common_file'
copying
 '../FileSystem/example/dir1/common_dir/dir1/file_in_dir1'
 to '/tmp/example/example/dir1/common_dir/dir1/file_in_dir1'
copying
 '../FileSystem/example/dir1/common_dir/dir1/file_only_in_dir1'
 to '/tmp/example/example/dir1/common_dir/dir1/file_only_in_dir1'
copying
 '../FileSystem/example/dir1/common_dir/dir1/not_the_same'
 to '/tmp/example/example/dir1/common_dir/dir1/not_the_same'
copying
 '../FileSystem/example/dir1/common_dir/dir2/common_file'
 to '/tmp/example/example/dir1/common_dir/dir2/common_file'
copying
 '../FileSystem/example/dir1/common_dir/dir2/file_only_in_dir2'
 to '/tmp/example/example/dir1/common_dir/dir2/file_only_in_dir2'
copying
 '../FileSystem/example/dir1/common_dir/dir2/not_the_same'
 to '/tmp/example/example/dir1/common_dir/dir2/not_the_same'
copying
 '../FileSystem/example/dir1/common_file'
 to '/tmp/example/example/dir1/common_file'
copying
 '../FileSystem/example/dir1/file_in_dir1'
 to '/tmp/example/example/dir1/file_in_dir1'
copying
 '../FileSystem/example/dir1/file_only_in_dir1'
 to '/tmp/example/example/dir1/file_only_in_dir1'
copying
 '../FileSystem/example/dir1/not_the_same'
 to '/tmp/example/example/dir1/not_the_same'
copying
 '../FileSystem/example/dir2/common_dir/dir1/common_file'
 to '/tmp/example/example/dir2/common_dir/dir1/common_file'
copying
 '../FileSystem/example/dir2/common_dir/dir1/file_in_dir1'
 to '/tmp/example/example/dir2/common_dir/dir1/file_in_dir1'
copying
 '../FileSystem/example/dir2/common_dir/dir1/file_only_in_dir1'
 to '/tmp/example/example/dir2/common_dir/dir1/file_only_in_dir1'
copying
 '../FileSystem/example/dir2/common_dir/dir1/not_the_same'
 to '/tmp/example/example/dir2/common_dir/dir1/not_the_same'
copying
 '../FileSystem/example/dir2/common_dir/dir2/common_file'
 to '/tmp/example/example/dir2/common_dir/dir2/common_file'
copying
 '../FileSystem/example/dir2/common_dir/dir2/file_only_in_dir2'
 to '/tmp/example/example/dir2/common_dir/dir2/file_only_in_dir2'
copying
 '../FileSystem/example/dir2/common_dir/dir2/not_the_same'
 to '/tmp/example/example/dir2/common_dir/dir2/not_the_same'
copying
 '../FileSystem/example/dir2/common_file'
 to '/tmp/example/example/dir2/common_file'
copying
 '../FileSystem/example/dir2/file_only_in_dir2'
 to '/tmp/example/example/dir2/file_only_in_dir2'
copying
 '../FileSystem/example/dir2/not_the_same'
 to '/tmp/example/example/dir2/not_the_same'
copying
 '../FileSystem/examples/shutil.ipynb'
 to '/tmp/example/examples/shutil.ipynb'
copying
 '../FileSystem/file_to_change.txt'
 to '/tmp/example/file_to_change.txt'
copying
 '../FileSystem/filecomp.ipynb'
 to '/tmp/example/filecomp.ipynb'
copying
 '../FileSystem/glob.ipynb'
 to '/tmp/example/glob.ipynb'
copying
 '../FileSystem/io.ipynb'
 to '/tmp/example/io.ipynb'
copying
 '../FileSystem/lorem.txt'
 to '/tmp/example/lorem.txt'
copying
 '../FileSystem/lorem_copy.txt'
 to '/tmp/example/lorem_copy.txt'
copying
 '../FileSystem/mmap.ipynb'
 to '/tmp/example/mmap.ipynb'
copying
 '../FileSystem/Path.ipynb'
 to '/tmp/example/Path.ipynb'
copying
 '../FileSystem/shutil.copy.ipynb'
 to '/tmp/example/shutil.copy.ipynb'
copying
 '../FileSystem/shutil.ipynb'
 to '/tmp/example/shutil.ipynb'
copying
 '../FileSystem/tempfile.ipynb'
 to '/tmp/example/tempfile.ipynb'

AFTER:
['/tmp/example/example',
 '/tmp/example/examples',
 '/tmp/example/file_to_change.txt',
 '/tmp/example/filecomp.ipynb',
 '/tmp/example/fnmatch',
 '/tmp/example/glob.ipynb',
 '/tmp/example/io.ipynb',
 '/tmp/example/lorem.txt',
 '/tmp/example/lorem_copy.txt',
 '/tmp/example/mmap.ipynb',
 '/tmp/example/Path.ipynb',
 '/tmp/example/shutil.copy.ipynb',
 '/tmp/example/shutil.ipynb',
 '/tmp/example/tempfile.ipynb']

In [15]:
import glob
import pprint
import shutil
print('Before:')
pprint.pprint(glob.glob('/tmp/example/*'))
shutil.rmtree('/tmp/example')
print('\nAfter')
pprint.pprint(glob.glob('/tmp/example/*'))


Before:
['/tmp/example/example',
 '/tmp/example/examples',
 '/tmp/example/file_to_change.txt',
 '/tmp/example/filecomp.ipynb',
 '/tmp/example/fnmatch',
 '/tmp/example/glob.ipynb',
 '/tmp/example/io.ipynb',
 '/tmp/example/lorem.txt',
 '/tmp/example/lorem_copy.txt',
 '/tmp/example/mmap.ipynb',
 '/tmp/example/Path.ipynb',
 '/tmp/example/shutil.copy.ipynb',
 '/tmp/example/shutil.ipynb',
 '/tmp/example/tempfile.ipynb']

After
[]

In [16]:
import glob
import shutil
with open('example.txt', 'wt') as f:
    f.write('content')
print('Before:', glob.glob('example*'))
shutil.move('example.txt', 'example.out')
print('After:', glob.glob('example*'))


Before: ['example', 'example.txt', 'examples']
After: ['example', 'example.out', 'examples']

Find Files


In [17]:
import shutil
print(shutil.which('virtualenv'))


/usr/local/bin/virtualenv

In [18]:
print(shutil.which('java'))


/usr/bin/java

In [20]:
print(shutil.which('tox'))


None

Archives


In [21]:
import shutil
for fm, description in shutil.get_archive_formats():
    print('{:<5}:{}'.format(fm, description))


bztar:bzip2'ed tar-file
gztar:gzip'ed tar-file
tar  :uncompressed tar file
xztar:xz'ed tar-file
zip  :ZIP file

In [26]:
import logging
import shutil
import sys
import tarfile
logging.basicConfig(
    format='%(message)s',
    stream=sys.stdout,
    level=logging.DEBUG
)
logger = logging.getLogger('shutil')
print('Creating archive')
shutil.make_archive(
    'example', 'gztar',
    root_dir='..',
    base_dir='FileSystem',
    logger=logger,
)
print('\nArchive contents')
with tarfile.open('example.tar.gz', 'r') as t:
    for n in t.getnames():
        print(n)


Creating archive
changing into '..'
Creating tar archive
changing back to '/Users/gaufung/WorkSpace/PythonStandardLibrary/FileSystem'

Archive contents
FileSystem
FileSystem/.ipynb_checkpoints
FileSystem/.ipynb_checkpoints/filecomp-checkpoint.ipynb
FileSystem/.ipynb_checkpoints/glob-checkpoint.ipynb
FileSystem/.ipynb_checkpoints/io-checkpoint.ipynb
FileSystem/.ipynb_checkpoints/mmap-checkpoint.ipynb
FileSystem/.ipynb_checkpoints/Path-checkpoint.ipynb
FileSystem/.ipynb_checkpoints/shutil-checkpoint.ipynb
FileSystem/.ipynb_checkpoints/tempfile-checkpoint.ipynb
FileSystem/example
FileSystem/example/dir1
FileSystem/example/dir1/common_dir
FileSystem/example/dir1/common_dir/dir1
FileSystem/example/dir1/common_dir/dir1/common_dir
FileSystem/example/dir1/common_dir/dir1/common_file
FileSystem/example/dir1/common_dir/dir1/dir_only_in_dir1
FileSystem/example/dir1/common_dir/dir1/file_in_dir1
FileSystem/example/dir1/common_dir/dir1/file_only_in_dir1
FileSystem/example/dir1/common_dir/dir1/not_the_same
FileSystem/example/dir1/common_dir/dir2
FileSystem/example/dir1/common_dir/dir2/common_dir
FileSystem/example/dir1/common_dir/dir2/common_file
FileSystem/example/dir1/common_dir/dir2/dir_only_in_dir2
FileSystem/example/dir1/common_dir/dir2/file_in_dir1
FileSystem/example/dir1/common_dir/dir2/file_only_in_dir2
FileSystem/example/dir1/common_dir/dir2/not_the_same
FileSystem/example/dir1/common_file
FileSystem/example/dir1/dir_only_in_dir1
FileSystem/example/dir1/file_in_dir1
FileSystem/example/dir1/file_only_in_dir1
FileSystem/example/dir1/not_the_same
FileSystem/example/dir2
FileSystem/example/dir2/common_dir
FileSystem/example/dir2/common_dir/dir1
FileSystem/example/dir2/common_dir/dir1/common_dir
FileSystem/example/dir2/common_dir/dir1/common_file
FileSystem/example/dir2/common_dir/dir1/dir_only_in_dir1
FileSystem/example/dir2/common_dir/dir1/file_in_dir1
FileSystem/example/dir2/common_dir/dir1/file_only_in_dir1
FileSystem/example/dir2/common_dir/dir1/not_the_same
FileSystem/example/dir2/common_dir/dir2
FileSystem/example/dir2/common_dir/dir2/common_dir
FileSystem/example/dir2/common_dir/dir2/common_file
FileSystem/example/dir2/common_dir/dir2/dir_only_in_dir2
FileSystem/example/dir2/common_dir/dir2/file_in_dir1
FileSystem/example/dir2/common_dir/dir2/file_only_in_dir2
FileSystem/example/dir2/common_dir/dir2/not_the_same
FileSystem/example/dir2/common_file
FileSystem/example/dir2/dir_only_in_dir2
FileSystem/example/dir2/file_in_dir1
FileSystem/example/dir2/file_only_in_dir2
FileSystem/example/dir2/not_the_same
FileSystem/example.out
FileSystem/examples
FileSystem/examples/shutil.ipynb
FileSystem/file_to_change.txt
FileSystem/filecomp.ipynb
FileSystem/fnmatch
FileSystem/fnmatch/.ipynb_checkpoints
FileSystem/fnmatch/fnmatch_filter.py
FileSystem/fnmatch/fnmatch_fnmatch.py
FileSystem/fnmatch/fnmatch_fnmatchcase.py
FileSystem/fnmatch/fnmatch_translate.py
FileSystem/glob.ipynb
FileSystem/io.ipynb
FileSystem/lorem.txt
FileSystem/lorem_copy.txt
FileSystem/mmap.ipynb
FileSystem/Path.ipynb
FileSystem/shutil.copy.ipynb
FileSystem/shutil.ipynb
FileSystem/tempfile.ipynb

File System Space


In [28]:
import shutil

total_b, used_b, free_b = shutil.disk_usage('.')

gib = 2 ** 30  # GiB == gibibyte
gb = 10 ** 9   # GB == gigabyte

print('Total: {:6.2f} GB  {:6.2f} GiB'.format(
    total_b / gb, total_b / gib))


Total: 120.10 GB  111.85 GiB

In [29]:
print('Used : {:6.2f} GB  {:6.2f} GiB'.format(
    used_b / gb, used_b / gib))
print('Free : {:6.2f} GB  {:6.2f} GiB'.format(
    free_b / gb, free_b / gib))


Used :  68.22 GB   63.53 GiB
Free :  51.62 GB   48.08 GiB