nikkiy nikkiy - 1 month ago 6
Python Question

Moving time-range specific files in Python

I'm new to programming, and am working on an assignment having to do with creating a script using Python to detect files from one folder created or modified in the past 24 hours, and then move those files to a different directory.

I've written a code that prints the files' creation and modification date, within
a function that also sorts out the older files/modified more than 24 hours ago. But I'm struggling with creating a script that actually takes those specific files and transfers them to a new folder. This is the first part of my code:

import os,time
import datetime
import shutil


now = dt.datetime.now()
ago = now-dt.timedelta(hours=24)
strftime = "%H:%M %m/%d/%Y"
created = 'C:\\Users\\Jacquelin\e\Desktop\\created'
dest = 'C:\\Users\\Jacqueline\\Desktop\\dest'


for root, dirs,files in os.walk(created):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago:
print "True: ", fname, " at ", mtime.strftime("%H:%M %m/%d/%Y")
else:
print False

for fname in os.listdir(created):
if mtime > ago:
shutil.move(path, dest)
print "moved to dest", fname


I'm using shutil.move to transfer the files, and have tried a million things-- but can't figure it out! I'm also working on turning these into functions to simplify things. So far, this is what I have:

def find_info(): #this first func. works fine.
for root, dirs, files in os.walk(created):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago:
print True
else:
print False


For the "move" function, I feel like this is the right idea but, not knowing the variables to pass, I've left the parentheses empty. And that returns a crazy long list of booleans. So.

def move():
for fname in os.listdir(created):
path = os.path.join(root, fname)
if find_info(fname) == True:
shutil.move(path, dest)
print "moved to dest", fname

print find_info() #variable? is one even needed?
print move() #variable????

print os.listdir(dest)
print os.listdir(created)


I'm not sure what variable(s) to pass into the functions. Afterwards, I'm printing the directories to see if the files have indeed been transferred. The damned things won't move.

Thanks!!

Answer

You're almost here. Just invoke shutil.move function after you detected that particular file is eligible for moveing (i.e. before/after/instead of print("True: ", fname, " at ", mtime.strftime("%H:%M %m/%d/%Y"))). Something like this will work:

import os,time
import datetime
import shutil

import datetime as dt

now = dt.datetime.now()
ago = now-dt.timedelta(hours=24)
strftime = "%H:%M %m/%d/%Y"
created = 'here'
dest = 'there'

for root, dirs,files in os.walk(created):  
    for fname in files:
        path = os.path.join(root, fname)
        st = os.stat(path)    
        mtime = dt.datetime.fromtimestamp(st.st_mtime)
        if mtime > ago:
            print("True:  ", fname, " at ", mtime.strftime("%H:%M %m/%d/%Y"))
            shutil.move(path, dest)
            # this is actual move

The following snippet is all wrong and not necessary:

for fname in os.listdir(created):
    if mtime > ago:                   
        shutil.move(path, dest)
        print "moved to dest", fname

Here mtime, path and dest don't depend on loop variable fname, so it is not clear why we need loop here and what is intended behaviour of this fragment.

Note that that your function find_info() (which we actually don't need either) returns nothing: this function prints True or False instead of returning it.

Comments