[Stephen Montgomery-Smith]
Actually our programs do different things. My program changes the header in the file to match the name of the file. Your program changes the name of the file to match the header of the file.
Oops, I missed that difference. Here is the original statement: 1. We have the directory with "xdir_name" name 2. In this directory we have files with "xdir_name_xfile_name". 3. In the first line each file has a header with "xdir_name" line. The question is how to automatically compare file header "xdir_name" to directory name "xdir_name" and if the file header doesn't match change it accordingly to the directory name where it is stored. which is unclear about what is a constant and what is a variable. The original Perl code gives the interpretation that "xdir_name" is a constant, that "xfile_name" is a variable, and that the "_" between both is a constant.
#!/usr/bin/perl cd "xdir_name" while ($file =
) { $file =~ /xdir_name_(.*)/; $name = $1; open(FILE,$file) || die "Couldn't open file $file $!"; $first_line = <FILE>; $rest = join '',<FILE>; close FILE; open(FILE,"> $file") || die "Couln't write to file $file $!"; print FILE "$name\n$rest"; close FILE; }
Let me interpret that both "xdir_name" and "xfile_name" are variables, that the "_" between them is constant, and restate some Python for it. Still untested code! :-) import os, string xdir_name = "xdir_name" for base in os.listdir(xdir_name): if base[:len(xdir_name)+1] == xdir_name+'_': xfile_name = base[len(xdir_name)+1:] file = os.path.join(xdir_name, base) lines = open(file).readlines() if string.strip(lines[0]) != xdir_name: lines[0] = xdir_name + os.linesep open(file, 'w').writelines(lines) -- 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/