Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 1st, 2006, 2:50 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Moving files between folders

I need to move all .jpg files in one folder/directory that are older than a given date, let's say April 1, 2003, to another folder. How can I do this best in Python? Need ideas.

I am using Windows XP and have looked at glob.glob() and os.stat(). When you move a file do you have to use copy and remove?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 1st, 2006, 4:28 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Moving a file can be done through os.rename, I believe (at least in Linux, they can, and since the os module is meant to be OS independent, it should work on Windows, too). A little confusing, I agree, but those familiar with Unix will recognise that renames and moves are essentially synonymous.

The following code might solve your problem:
import os
import stat
from datetime import datetime

def modified_date(filename):
    return datetime.utcfromtimestamp(os.stat(filename)[stat.ST_MTIME])

move_date = datetime(2003, 4, 1)    # 1st April 2003
original_folder = ...
new_folder = ...

for filename in os.listdir(original_folder):
    full_filename = os.path.join(original_folder, filename)

    if modified_date(full_filename) < move_date and filename.endswith(".jpg"):
        os.rename(full_filename, os.path.join(new_folder, filename))
Arevos is offline   Reply With Quote
Old Jan 1st, 2006, 4:33 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
You'll wanna check out the module shutil. It's as simple as shutil.move or shutil.copytree.
Sane is offline   Reply With Quote
Old Jan 1st, 2006, 4:51 PM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thanks for the info! I am working on code.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 1st, 2006, 4:51 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Sane's probably right. The docs for shutil.move seem to imply that os.rename only works for files on the same filesystem, so os.rename might not be able to move files between drives, whilst shutil.move can.

Replace os.rename with shutil.move, then
Arevos is offline   Reply With Quote
Old Jan 10th, 2006, 10:27 PM   #6
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
A few suggested tweaks:
import os
import glob
import shutil
import time
from datetime import datetime

move_date = datetime(2003, 4, 1)    # 1st April 2003
move_date = time.mktime(move_date.timetuple())
original_folder = ...
new_folder = ...

for filename in glob.glob1(original_folder, "*.jpg"):
    srcfile = os.path.join(original_folder, filename)
    destfile = os.path.join(new_folder, filename)
    if os.stat(srcfile).st_mtime < move_date:
        shutil.move(srcfile, destfile)
--OH.
hydroxide is offline   Reply With Quote
Old Jan 12th, 2006, 9:41 AM   #7
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Thanks Hydroxide,

that is real smooth code! Getting the date formats to match for the comparison is the trick!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC