Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2012, 12:20 PM   PM User | #1
smarty
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
smarty is an unknown quantity at this point
SyntaxError

I need assistance with Python Programming
I need apply the XOR operation to file on a particular keyword. I found the function:
Code:
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

Last edited by smarty; 10-03-2012 at 12:31 PM..
smarty is offline   Reply With Quote
Old 01-10-2013, 02:14 PM   PM User | #2
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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:
Code:
if bytesToCopy > = len(keyData):
to this:
Code:
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
TFlan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 AM.


Advertisement
Log in to turn off these ads.