files
¶
Tools for file and directory management.
-
patchwork.files.
append
(*args, **kwargs)¶ Append string (or list of strings)
text
tofilename
.When a list is given, each string inside is handled independently (but in the order given.)
If
text
is already found infilename
, the append is not run, and None is returned immediately. Otherwise, the given text is appended to the end of the givenfilename
via e.g.echo '$text' >> $filename
.The test for whether
text
already exists defaults to a full line match, e.g.^<text>$
, as this seems to be the most sensible approach for the “append lines to a file” use case. You may override this and force partial searching (e.g.^<text>
) by specifyingpartial=True
.Because
text
is single-quoted, single quotes will be transparently backslash-escaped. This can be disabled withescape=False
.
-
patchwork.files.
contains
(*args, **kwargs)¶ Return True if
filename
containstext
(which may be a regex.)By default, this function will consider a partial line match (i.e. where
text
only makes up part of the line it’s on). Specifyexact=True
to change this behavior so that only a line containing exactlytext
results in a True return value.This function leverages
egrep
on the remote end (so it may not follow Python regular expression syntax perfectly), and skips the usual outerenv.shell
wrapper that most commands execute with.If
escape
is False, no extra regular expression related escaping is performed (this includes overridingexact
so that no^
/$
is added.)
-
patchwork.files.
directory
(*args, **kwargs)¶ Ensure a directory exists and has given user and/or mode
-
patchwork.files.
exists
(*args, **kwargs)¶ Return True if given path exists on the current remote host.