Skip to content

Instantly share code, notes, and snippets.

@brodul
Created February 3, 2012 11:29
Show Gist options
  • Save brodul/1729758 to your computer and use it in GitHub Desktop.
Save brodul/1729758 to your computer and use it in GitHub Desktop.
List files with fabric
from fabric.api import env, run, cd
env.hosts = ["localhost"]
def list_dir(dir=None):
"""docstring for list_dir"""
dir = dir or env.cwd
string = run("for i in %s*; do echo $i; done" % dir)
files = string.replace("\r","").split("\n")
return files
@brandonsturgeon
Copy link

Thanks! Very helpful for my current project!

@guysoft
Copy link

guysoft commented Feb 8, 2017

Makes it a little less verbose:

def list_dir(dir_=None):
    """returns a list of files in a directory (dir_) as absolute paths"""
    with hide('output'):
        if dir_ is not None and not dir_.endswith("/"):
            dir_ += "/"
        dir_ = dir_ or env.cwd
        string_ = run("for i in %s*; do echo $i; done" % dir_)
        files = string_.replace("\r","").split("\n")
    return files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment