Command Line Helpers

Functions to only be in an interactive instances to ease life.

cmd(command, ignore_stderr=False, raise_on_return=False, timeout=None, encoding='utf-8')

Run a shell command and have it automatically decoded and printed

Parameters:
  • command – Command to run as str
  • ignore_stderr – To not print stderr
  • raise_on_return – Run CompletedProcess.check_returncode()
  • timeout – timeout to pass to communicate if python 3
  • encoding – How the output should be decoded
pushd(directory)

Change working directories in style and stay organized!

Parameters:directory – Where do you want to go and remember?
Returns:saved directory stack
popd()

Go back to where you once were.

Returns:saved directory stack
pwd()

Get the current working directory

cd(directory)

Change working directory, with built in user (~) expansion

Parameters:directory – New place you wanted to go
ls(params='', directory='.', printed=True)

Know the best python implantation of ls? It’s just to subprocess ls… (uses dir on windows).

Parameters:
  • params – options to pass to ls or dir
  • directory – if not this directory
  • printed – If you’re using this, you probably wanted it just printed
Returns:

if not printed, you can parse it yourself

find(name=None, ext=None, directory='.', match_case=False, disable_glob=False, depth=None)

Designed for the interactive interpreter by making default order of find_files faster.

Parameters:
  • name – Part of the file name
  • ext – Extensions of the file you are looking for
  • directory – Top location to recursively search for matching files
  • match_case – If name has to be a direct match or not
  • disable_glob – Do not look for globable names or use glob magic check
  • depth – How many directories down to search
Returns:

list of all files in the specified directory

head(file_path, lines=10, encoding='utf-8', printed=True, errors='strict')

Read the first N lines of a file, defaults to 10

Parameters:
  • file_path – Path to file to read
  • lines – Number of lines to read in
  • encoding – defaults to utf-8 to decode as, will fail on binary
  • printed – Automatically print the lines instead of returning it
  • errors – Decoding errors: ‘strict’, ‘ignore’ or ‘replace’
Returns:

if printed is false, the lines are returned as a list

cat(file_path, encoding='utf-8', errors='strict')
Parameters:
  • file_path – Path to file to read
  • encoding – defaults to utf-8 to decode as, will fail on binary
  • errors – Decoding errors: ‘strict’, ‘ignore’ or ‘replace’
tail(file_path, lines=10, encoding='utf-8', printed=True, errors='strict')

A really silly way to get the last N lines, defaults to 10.

Parameters:
  • file_path – Path to file to read
  • lines – Number of lines to read in
  • encoding – defaults to utf-8 to decode as, will fail on binary
  • printed – Automatically print the lines instead of returning it
  • errors – Decoding errors: ‘strict’, ‘ignore’ or ‘replace’
Returns:

if printed is false, the lines are returned as a list

cp(src, dst, overwrite=False)

Copy files to a new location.

Parameters:
  • src – list (or string) of paths of files to copy
  • dst – file or folder to copy item(s) to
  • overwrite – IF the file already exists, should I overwrite it?