Enter IPython and its wonderful facility for mixing a little bit of shell goodness into your Python code. Look what worked:
In [3]: fs = !lsNeat, huh?
In [4]: fs
Out[4]: ['base.py', 'cache', '__init__.py', 'rss.py', 'util.py']
In [5]: for fn in fs:
...: if fn.endswith('.py'):
...: dest = 'test_' + fn
...: !git mv $fn $dest
...:
In [6]: !ls
cache test_base.py test___init__.py test_rss.py test_util.py
5 comments:
While your filenames should not contain spaces, if they do it will break your code.
Replace dest = 'test_' + fn with
dest = 'test_' + fn.replace(' ', '_'), or change the git command to
!git mv "$fn" "$dest"
For reference, the bash is
for i in *.py; do git mv "$i" "test_$i"; done
Oh hey! Comments on my blog post!? Huzzah! ^_^ I appreciate the bash stanza, that's exactly what I was trying to remember when I recalled Ipython's '!' magic. And yes, only truly evil people put spaces in their file names.
Based on vidir from moreutils, I made a "git-vidir" which makes these sorts of edits really easy (they degenerate to file edits in vim, or your editor of choice). Unlike the Python script, you get to review the changes before effecting them.
What I meant was, I just copied vidir and replaced the unlink and rename calls with git directives. It's only slightly better than just using vidir directly and then git add / git commit -a. vidir itself is the real winner.
More people need to know about vidir :)
Post a Comment