...

SyntaxError

smarty
10-03-2012, 12:20 PM
I need assistance with Python Programming
I need apply the XOR operation to file on a particular keyword. I found the function:
def xor(fileTarget, key):
fileTarget = fileTarget + os.listdir(fileTarget)[0]

ifile = os.path.split(fileTarget)[0]+"/xor.in"
ofile = os.path.split(fileTarget)[0]+"/xor.out"
os.rename(fileTarget, ifile)

if os.path.isfile(key):
kf = open(key)
keyData = kf.read()
kf.close()
else:
keyData = key

e = open(ofile, "w")
f = open(ifile, "r")

bytesToCopy = os.stat(os.path.split(fileTarget)[0]+"/xor.in")[6]

while bytesToCopy:

if bytesToCopy > = len(keyData):
data = f.read(len(keyData))
bytesToCopy -= len(keyData)
encryptedData = ''.join([chr(ord(data[i])^ord(keyData[i])) for i in xrange(len(data))])
# encryptedData = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
e.write(encryptedData)

else:
data = f.read(bytesToCopy)
bytesToCopy = 0
encryptedData = ''.join([chr(ord(data[i])^ord(keyData[i])) for i in xrange(len(data))])
# encryptedData = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
e.write(encryptedData)
os.remove(ifile)

e.close()
f.close()

return os.path.split(fileTarget)[0]+"/xor.out"

I run it in the terminal Ubuntu 12.04 LTS and get error "SyntaxError: invalid syntax". Error causes a semicolon on line 22 ( if bytesToCopy > = len(keyData): ). I tried to add and remove spaces, remove the semicolon - does not help.
What is wrong?
TIA

TFlan
01-10-2013, 02:14 PM
Not sure if you just copied and pasted this code, but you found your own problem and your own solution..

As you might guess > is an html entity of the character '>' - greater than

Change this line:
if bytesToCopy > = len(keyData):

to this:
if bytesToCopy >= len(keyData):

Please NEVER, EVER, EVER blindly copy and paste python.. always re-write it yourself. This will help you understand what you are trying to accomplish and will make sure some punk who posted that code isn't pulling your leg for a good, cheap laugh



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum