[PYTHON] Scripting with Paver-File Operations

Are you wondering if the file operations in the Python standard library are in os``, os.path, or shutil``?

The path class in the paver.path module makes file operations easier than the standard Python library.

How to use

The path class is also included in paver.easy, so if you do from paver.easy import *, you can use it only with path.

This path class defines a method that inherits a string and takes that string as an argument. It also overrides the join path operation on the / operator and defines various other useful methods.

>>> from paver.easy import *
>>> root = path('/')
>>> root
path(u'/')
>>> root.isdir()
True
>>> root.listdir()
[path(u'/.dbfseventsd'), path(u'/.DocumentRevisions-V100'), path(u'/.DS_Store'), path(u'/.file'), path(u'/.fseventsd'), path(u'/.Spotlight-V100'), path(u'/.SymAVx86QSFile'), path(u'/.Trashes'), path(u'/.vol'), path(u'/Applications'), path(u'/asix_temp'), path(u'/bin'), path(u'/cores'), path(u'/dev'), path(u'/etc'), path(u'/home'), path(u'/Library'), path(u'/mach_kernel'), path(u'/net'), path(u'/Network'), path(u'/opt'), path(u'/private'), path(u'/sbin'), path(u'/System'), path(u'/tmp'), path(u'/Users'), path(u'/usr'), path(u'/var'), path(u'/Volumes'), path(u'/\u30e6\u30fc\u30b5\u3099\u60c5\u5831')]
>>> root / 'usr'
path(u'/usr')

For convenient methods other than path operations in os``, os.path, shutil, read and return the byte string in the file specified by the path .bytes () There are methods, glob () methods, and .remove_p () methods that do not cause an error even if the file does not exist, unlike .remove () ``.

dryrun

The paver command is in dryrun mode with the -n option, and the manipulation methods of the path class actually do nothing.

pavement.py


from paver.easy import *

@task
def hello():
    path('/').rmtree()
$ paver -n hello
---> pavement.hello
rmtree / () {}

pushd This is a feature originally found in Python's contextlib, but there is a context manager called pushd that is useful in combination with the with statement, and it goes into paver.easy. You can use it immediately because it is.

pavement.py


from paver.easy import *
import os

@task
def chdir():
    print "cwd: ", os.getcwd()
    with pushd('/'):
        print "cwd: ", os.getcwd()
    print "cwd: ", os.getcwd()
$ paver chdir
---> pavement.chdir
cwd:  /Users/inada-n/paver
cd /
cwd:  /
cd /Users/inada-n/paver
cwd:  /Users/inada-n/paver

Recommended Posts

Scripting with Paver-File Operations
File operations with open — "../"
Scripting with Paver-Command Definition
Scripting with Paver-External Commands
Perform logical operations with Perceptron
Automation of remote operations with Fabric
Summary of operations often performed with asyncpg