Ruby:
filename
=
ARGV[0]
rf = File.new(filename, " r " )
wf = File.new( " #{filename}.txt " , " w " )
i = 0
for line in rf.readlines
i = i + 1
snum = i.to_s + " : "
line = snum + line
wf.puts(line)
end
rf.close
wf.close
print " Finished! "
rf = File.new(filename, " r " )
wf = File.new( " #{filename}.txt " , " w " )
i = 0
for line in rf.readlines
i = i + 1
snum = i.to_s + " : "
line = snum + line
wf.puts(line)
end
rf.close
wf.close
print " Finished! "
Python:
import
sys
filename = sys.argv[ 1 ]
rf = open(filename, " r " )
wf = open(filename + " .txt " , " w " )
i = 0
for line in rf.readlines():
i = i + 1
snum = " %s: " % i
line = snum + line
wf.write(line)
rf.close()
wf.close()
print " Finished! "
filename = sys.argv[ 1 ]
rf = open(filename, " r " )
wf = open(filename + " .txt " , " w " )
i = 0
for line in rf.readlines():
i = i + 1
snum = " %s: " % i
line = snum + line
wf.write(line)
rf.close()
wf.close()
print " Finished! "
两个程序都是通过命令行参数引入待处理的文件,将处理后加上了行号的行写入到另一个文件中(原文件名+“.txt”)。