0

I will try to explain a little more clearly: I am trying to figure out how to use shutil and os modules on Python 3.8.5 to be able to take a look at a folder, determine if its contents have been created and/or modified within the last 24 hours... and then if they have, move those files to another folder.

I am going to try to link up the code I have here, I'm still pretty new at using Stackoverflow, so I apologize:

import shutil
import os

shutil.copystat(' \Users\aaron\Desktop\checkFiles\File B.txt', "\Users\aaron\Desktop\needToCopy\"," follow_symlinks=True)

This code keeps giving me invalid syntax errors. I don't know what I'm doing wrong, I have even looked at docs.python.org, but, since I am very new to coding, it was pretty greek to me.

1 Answers1

0

I am not sure, what are you trying to achieve by using shutil.copystat. It only copies the stats and permissions onto the path. (If your File B.txt is read only, the needToCopy will be also read only)

In order to find out creation and modification times, consult this great answer.

I would approach the check for 24 hour time window modification like this:

import os, time
DIR_PATH = "."
for filename in os.listdir(DIR_PATH):
    if os.path.getmtime(filename) >= (time.time() - 60*60*24): print(filename)

And for the moving part, there is (for example) shutil.move. So it could look like this:

import os, time, shutil
SRC_PATH = "."
TARGET_PATH = "../"
for filename in os.listdir(SRC_PATH):
    if os.path.getmtime(filename) >= (time.time() - 60*60*24): 
        shutil.move(os.path.join(SRC_PATH, filename), TARGET_PATH)  
Minarth
  • 314
  • 2
  • 9