Moving files
Erik Price
eprice at ptc.com
Mon Jan 6 17:35:57 EST 2003
Kevin D. Clark wrote:
> Erik Price <eprice at ptc.com> writes:
>
>
>>PS: FWIW, Python is a friendlier and IMO superior language for writing
>>scripts where legibility is important, but you can't write a oneliner
>>like the OP's Perl script using Python. I think a lot of Perl users
>>like the way that it is quick to write.
>
>
> Well, again, everybody is entitled to their opinions, but I find that
> I am quite successful at writing legible Perl scripts. I've been
> playing around with Python for a while now, but I still prefer Perl
> over Python.
Strange, you sound like you are at odds with me but I never questioned
anyone's ability to write legible Perl scripts. In fact, I always jump
ahead to Randal's Perl column in Linux Magazine, because his code is
often the simplest and easiest to read of all the source code published
in that magazine.
I did say, however, that Python is superior in my opinion for writing
scripts where legibility is important. No offense, but I wasn't
responding to you at all, but to Derek, who was saying something about
Perl not being easy to read by programmers who don't know the
idiosyncracies of Perl, or something. In that case, yes, Python's a lot
more straightforward -- as some people like to say, it's "executable
pseudocode".
> BTW, where's your Python solution to this problem? Please make sure
> that it is functionally identical to the solution that I posted.
I didn't read the details of File::Find (which is too bad since I've
heard of it before and it looks like a great module), so I'm not sure if
this does everything, but I managed to hash this script out. I think it
does the same thing. Let me know if it doesn't. Also note that Python
offers the list comprehensions and map and other elegant functional
programming features that Perl has, but I didn't use them as some of the
discussion on this list suggested that they aren't very readable.
(There are definitely less verbose ways to write unspacify.py.
Criticism welcome.)
Erik
#!/usr/bin/python
import sys
import os
import os.path
def unspacify(somestring):
return somestring.replace(' ', '_')
def unspacifyFile(filename):
if os.path.isfile(filename):
os.rename(filename, unspacify(filename))
# this next condition will never happen if this
# function is called from unspacifyDirectory(),
# but is included in case this function is invoked
# directly (from another script)
elif os.path.isdir(filename):
os.rename(filename, unspacify(filename))
else:
raise IOError
def unspacifyDirectory(directoryname, recurse=False):
if os.path.isdir(directoryname):
thisdir = os.getcwd()
os.chdir(directoryname)
for file in os.listdir("."):
if os.path.isdir(file) and recurse == True:
unspacifyDirectory(file, False)
try:
unspacifyFile(file)
except IOError, ioe:
print "Unreadable: " + file
continue
os.chdir(thisdir)
def usage():
print "usage: unspacify.py [-r] targetfile"
if __name__ == '__main__':
if sys.argv[1] == '-r' and os.path.isdir(sys.argv[2]):
unspacifyDirectory(sys.argv[2], True)
elif os.path.isdir(sys.argv[1]):
unspacifyDirectory(sys.argv[1])
elif os.path.isfile(sys.argv[1]):
try:
unspacifyFile(sys.argv[1])
except IOError, ioe:
print "File Unreadable: " + sys.argv[1]
sys.exit(1)
else:
usage()
sys.exit(1)
sys.exit(0)
More information about the gnhlug-discuss
mailing list