Help a newbie pls!

Rainy sill at optonline.net
Mon Jun 4 16:38:25 EDT 2001


On Mon, 4 Jun 2001 20:52:58 +0100, David A. <man_at_moon at hotmail.com> wrote:
> Hi to all,
> 
> I am a newbie to python and I was writing a very simple script taht takes
> the content of one file and then re-writes it in the H4x0r style (i know, I
> know, it's completly stupid and useless, and, no I am no of them, it is just
> a toy program...) the problem is it doesn't work, could anyone look at the
> few lines and explain me why?
> 
> in the source code I say you where it stops to work.
> 
> thanks for any help in advance!
> 
> 
> 

There you go (I explain everything in comments):


#!/usr/bin/env python

#31172 H4x0R
# 
# 	or
#
#		Fancy writing!


import tempfile
import sys
import string


def main():
	print "hello"
	try:file=open('FAQ.txt')
	except IOError:
		print "Unable to open file", file
		sys.exit(4)
	#try:
	temp_file=open('res.txt','w') #tempfile.mktemp()
	#except IOError, msg:
	#	print "Unable to create temp file!"
	#	print msg
	#	sys.exit(2)
	x=0 
        # that's the correct (and accepted) way to write it
        # it's not as clean, so 2.2 will have a cleaner version,
        # and 2.1 already has a better version using .xreadlines()
        # but I don't know which version you're using..
        # in 2.1 i think it's while line in file.xreadline(): or something similar
        while 1:
            line = hx0(file.readline())
            if not line: break
            temp_file.write(line)
            x = x+1
            print x
	sys.exit(0)
	
def hx0(line):
	new_line=[]
	for letter in line:
		if letter=='i':letter='1'
		if letter=='a':letter='4' 
		if letter=='o':letter='0'
		if letter=='x':letter='X'
		new_line.append(letter)
                # otherwise, join inserts a space between joined elements
	return string.join(new_line,'') 
#	print new_line
main()	


-- 
I'll give you anything, everything if you want things
        - Syd



More information about the Python-list mailing list