When you write a little script, you assemble and execute a string, right?
Something like this (I don't use shutil
because it's an example)
os.system('mv %(src)s %(dst)s' % locals())
Such a guy
os.system('ssh %(user)s@%(host)s -p %(port)s hostname' % locals())
So, have you ever done this when you want to check the assembled string? It's a hassle
# os.system('mv %(src)s %(dst)s' % locals())
print 'mv %(src)s %(dst)s' % locals()
If you want to debug like this, it's easier to replace ʻos.system`
def out(s):
print s
os.system = out
os.system('mv %(src)s %(dst)s' % locals())
Now that ʻos.systemis actually
print`, when you run it, the assembled string will be output as standard and you can debug it easily.
By the way, python2 series uses def
because print
is a sentence, but python3 series is easier because print
is an expression.
os.system = print
os.system('mv %(src)s %(dst)s' % locals())
If it is a first-class object language, it will be applied, so it may be good to remember
Recommended Posts