Skip to content

Instantly share code, notes, and snippets.

@vmx
Last active December 20, 2015 07:39
Show Gist options
  • Save vmx/6095160 to your computer and use it in GitHub Desktop.
Save vmx/6095160 to your computer and use it in GitHub Desktop.
Creates directories if they didn't exist before the file is moved. This script is meant to be uses in conjunction with `qmv`. That way it's possible to rename files from foo.txt -> bar/baz.txt. Just run qmv as `qmv --command=pmv.py`
#!/usr/bin/env python3
# pmv.py - Creates directories if they didn't exist before the file is moved
# Written in 2013 by Volker Mische <volker.mische@gmail.com>
#
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along
# with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
# This script is meant to be uses in conjunction with `qmv`.
# That way it's possible to rename files from foo.txt -> bar/baz.txt.
# Just run qmv as `qmv --command=pmv.py`
import os
import sys
src, dst = sys.argv[2:]
try:
os.rename(src, dst)
except OSError as ex:
if ex.errno == 2:
os.makedirs(os.path.dirname(dst))
os.rename(src, dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment