Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: SyntaxError

  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 01:31 PM.

  • #2
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •