I have just started to learn python (Learning Python), and I am trying to move some files to another folder that are over a week old. I keep getting an error about the files not existing.

import shutil, sys, time, os
src = 'c:/users/wca36050/temp1'
dst = 'c:/users/wca36050/temp2'

now = time.time()
for f in os.listdir(src):
if os.stat(f).st_mtime < now - 7 * 86400:
if os.path.isfile(f):
shutil.move(f, dst)

Error I am getting: "WindowsError: [Error 2] The system cannot find the file specified: 'pic.jpg'"

The file does exist. I know I am probably doing something stupid. Any ideas?

Thanks!

Recommended Answers

All 2 Replies

EDIT: Code tags added
I have just started to learn python (Learning Python), and I am trying to move some files to another folder that are over a week old. I keep getting an error about the files not existing.

import shutil, sys, time, os
src = 'c:/users/wca36050/temp1'
dst = 'c:/users/wca36050/temp2'

now = time.time()
for f in os.listdir(src):
    if os.stat(f).st_mtime < now - 7 * 86400:
        if os.path.isfile(f):
            shutil.move(f, dst)

Error I am getting: "WindowsError: [Error 2] The system cannot find the file specified: 'pic.jpg'"

The file does exist. I know I am probably doing something stupid. Any ideas?

Thanks!

The code worked somehow, I am not sure reason of your problems. I would express the things little differently though (without real move for safety):

import shutil, sys, time, os
src, dst = 'd:/test', 'c:/temp'
secondinaday = 24 * 60 * 60

def olderthan(days,ftime):
    return (ftime < (time.time() - days * secondinaday))

move_these = [f for f in os.listdir(src) if (os.path.isfile(f) and newerthan(days = 7, os.stat(f).st_mtime))
              ]
            
## real move here
print move_these
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.