QUick help with python syntax error.

oToom

Newbie
Joined
Oct 4, 2009
Messages
24
Reaction score
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.
 
I would think that the line should be written instead as

shutil.move(folder + "\\" + filename, destination)
 
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:
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
 
Ah its jpg not jpeg by the looks of things.
 
I read your recent pm.
I can't reply because I
don't have 15 posts yet.
 
Oh right mate, answer on here it's fine haha. If you can :)
 
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.
 
Back
Top