![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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)) |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
You'll wanna check out the module shutil. It's as simple as shutil.move or shutil.copytree.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Thanks for the info! I am working on code.
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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 ![]() |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
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) |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|