Skip to content

Instantly share code, notes, and snippets.

@rainyear
Last active January 1, 2016 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainyear/8215756 to your computer and use it in GitHub Desktop.
Save rainyear/8215756 to your computer and use it in GitHub Desktop.
map a command(function) to all the files in current directory.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
Walking the destination directory(arg3) and using the command(arg2)
to process every file.
USAGE:
Mapwalk `function` /path/to/walk
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os.path
def walk_dir(dir, cmd="", topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
opf = os.path.join(name)
exe = "%s '%s'" % (cmd, opf)
print exe
os.system(exe)
if __name__ == '__main__':
walk_dir("./", sys.argv[1])
@markguo
Copy link

markguo commented Feb 5, 2014

直接用find就可以了吧,find的过滤功能还更强大

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