Interactive rename script in Python
For a while I've been using the rather useful file renaming utilities by Oskar Liljeblad for moving and copying files. However, although the program works fine for most purposes, it hasn't been updated for开发者_高级运维 a few years and it's not Unicode-compatible—non ASCII characters are replaced with C escaped strings. This makes interactively moving/copying/renaming files a real nuisance at times (i.e. I have a number of files saved with Cyrillic file names).
The principle is simple: give the program a list of files to rename or copy, it drops you into an editor with the list of files, you edit that list, save and exit, and the program then applies those changes, with a limited amount of error-handling to catch common cases.
I've contemplated re-writing his programs in Python 3, but I'm not entirely sure how to go about it, given I want to completely re-implement them. I've got a few ideas, but turning ideas into (working) code is proving somewhat problematic. As I've been thinking of learning Python, I figured this would be a nice little project.
Any tips on where to start?
This sounds like an excellent project! You might want to start with the shutil module. You might also be interested in the os.path module.
Perhaps to get you started, here's a function that takes a path to a file and backs the file up by copying it and giving it a .bak extension.
import os, shutil
def backup(path):
if not os.path.exists(path):
print "No such file:", path
return
try:
shutil.copy(path, path + ".bak")
except:
print "There was an error when backing up", path
backup('/path/to/file')
精彩评论