[Koos Pol]
An enhanced version may look like this:
# Move a file to the directory pointed to by it's very first line. while ($d=
) { while ($f = <$d/xfile_.*>) { open (F, $f) || (next, die "Can't open $f ($!)\n"; $firstline = <F>; close (F); chomp $firstline; ($firstline ne $d) && (rename ($f, "$firstline/$f") || die "Can't move $f to $firstline/$f ($!)\n"); } }
Can't we make a contest out of it? A virtual cool beer for the next successful contender! Cheers,
A cool beer, ah! :-) Here is a translation of the above in Python (untested): import os for dir in os.listdir('.'): if dir[:5] == 'xdir_': for base in os.listdir(dir): if base[:7] == 'xfile_.': file = os.path.join(dir, base) newdir = open(file).readline()[:-len(os.linesep())] if newdir != dir: os.rename(file, os.path.join(newdir, base)) Let me remark a few things. There is no need to check and die as in Perl, as this is the default action (one catches errors when needed). The portable "-len(os.linesep())" could more simply be written "-1" on Unix. I do not need to close explicitly, because I know that C Python will close immediately unless I keep a reference to the file, which I do not. Python does not formally specify this, but it makes writing so clearer that I abuse of this detail (which is there to stay in (C)Python, but is not correct in JPython). Finally, the "[:5]" and"[:7]" could be replaced by more elegant writings in Python 1.6, but I wanted to give a translation of Perl that would run on 1.5. -- François Pinard http://www.iro.umontreal.ca/~pinard -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/