1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

QUick help with python syntax error.

Discussion in 'Other Languages' started by oToom, Jan 12, 2013.

  1. oToom

    oToom Newbie

    Joined:
    Oct 4, 2009
    Messages:
    24
    Likes Received:
    1
    Hey all, just trying to make a simple script in Python and getting an error.
    Wondered if anyone would be kind enough to help me out.
    All this is doing (or what im hoping its going to do) is run through a folder, checking if the extension of a file is whats given, if it is, it will move the file.

    Code:
    #! /usr/bin/env python
    
    import sys
    import os
    import shutil 
    
    folder = os.path.join('F:\\', 'Tumblr')
    destination = os.path.join('F:\\', 'jpeg')
    extmove = 'jpeg'
    
    for filename in os.listdir(folder):
            extension = filename.split(".")[-1]
            if extension == extmove:
                shutil.move(folder."\\".filename, destination)
                    
                    
                    
    My error is at this line
    shutil.move(folder."\\".filename, destination)

    Points to the second quotation mark "invalid syntax.


    Help appreciated.
     
  2. james344

    james344 Newbie

    Joined:
    Jan 10, 2013
    Messages:
    4
    Likes Received:
    0
    I would think that the line should be written instead as

    shutil.move(folder + "\\" + filename, destination)
     
  3. oToom

    oToom Newbie

    Joined:
    Oct 4, 2009
    Messages:
    24
    Likes Received:
    1
    Thank you I will give that a go.

    EDIT:

    Okay so this is my code.

    Code:
    #! /usr/bin/env python
    
    import sys
    import os
    import shutil 
    
    folder = os.path.join('F:\\', 'Tumblr')
    destination = os.path.join('F:\\', 'jpeg')
    extmove = 'jpeg'
    num = 0
    
    for filename in os.listdir(folder):
            extension = filename.split(".")[-1]
            if extension == extmove:
                shutil.move(folder + "\\" + filename, destination + "\\" + filename)
            num = num + 1
            print num
                    
                    
                    
    It doesn't seem to do anything. Am i missing something here?
     
    Last edited: Jan 12, 2013
  4. james344

    james344 Newbie

    Joined:
    Jan 10, 2013
    Messages:
    4
    Likes Received:
    0
    insert a print statement to see why you are not getting a match

    for filename in os.listdir(folder):
    extension = filename.split(".")[-1]
    print filename, extension
     
  5. oToom

    oToom Newbie

    Joined:
    Oct 4, 2009
    Messages:
    24
    Likes Received:
    1
    Ah its jpg not jpeg by the looks of things.
     
  6. james344

    james344 Newbie

    Joined:
    Jan 10, 2013
    Messages:
    4
    Likes Received:
    0
    Excellent.
    You got it.
     
  7. james344

    james344 Newbie

    Joined:
    Jan 10, 2013
    Messages:
    4
    Likes Received:
    0
    I read your recent pm.
    I can't reply because I
    don't have 15 posts yet.
     
  8. oToom

    oToom Newbie

    Joined:
    Oct 4, 2009
    Messages:
    24
    Likes Received:
    1
    Oh right mate, answer on here it's fine haha. If you can :)
     
  9. Question

    Question Registered Member

    Joined:
    Aug 14, 2011
    Messages:
    51
    Likes Received:
    32
    Don't know if you solved the problem already, but here is a snippet to use instead of print statement.

    Code:
    import pdb; pdb.set_trace()
    This will show a debug console, you can print variables and run code right there as the program runs. Hit 'n' for the next statement and just type variable names to see their contents.