0

Please excuse my ignorance, I am new to programming and python, the code below allows me copy file if and only if it was modified in the last 24 hours.

Is there a better way I can twist my program to consider also the last 8 character which is the date the file was created _20191108. Files are usually as presented below

  • 7***_13_01_2172_20191106.txt
  • 7***_13_01_2174_20191107.txt
  • 7***_12_01_2175_20191108.txt
  • 7***_13_01_2176_20191108.txt

    import time
    import os
    import shutil
    
    giorno = 24 * 60 * 60
    
    src = 'C:/Users/Daniels/Desktop/FileMover/SourceA'
    dst = 'C:/Users/Daniels/Desktop/FileMover/SourceB'
    
    now = time.time()
    primo = now - giorno
    
    def last_mod_time(file_name):
        return os.path.getmtime(file_name)
    
    for file_name in os.listdir(src):
        src_filename = os.path.join(src, file_name)
        if last_mod_time(src_filename) > primo:
            dst_filename = os.path.join(dst, file_name)
            shutil.copy(src_filename, dst_filename)
            print(file_name)
    

Thank you!

J. Murray
  • 1,460
  • 11
  • 19
  • Do you want to determine the creation date only by using the file name? – alec_djinn Nov 11 '19 at 15:19
  • This might be an XY problem. Why are you using timestamps in the filenames? There are means of gettign the file creation time depending on what system you're running. [os.stat](https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python) can make this much easier and reliable. – Brian Nov 11 '19 at 15:30
  • @BrianJoseph OP is already using `os.path.getmtime()` here... – AKX Nov 11 '19 at 17:21
  • @AKX, yeah you're right. I guess I'm just confused because OP is talking about deriving the *creation* date from the filename but is only extracting the *modification* data using `os`. – Brian Nov 11 '19 at 20:24
  • Thanks for stepping in guys. @alec_djinn, the file is created in a way where 20191106.txt is the date of creation or uploaded in the server. As Brain said, os.path.getmtime() gives me the time of last modification. my goal is to be able to copy files based on fle date considering today will 20191112.txt – DanielKing Nov 12 '19 at 10:12
  • I'd say your code is fine as it is - you only use utc timestamps, so all good. Could you clarify on what you intend to do with the date that can be extracted from the file name? – FObersteiner Nov 12 '19 at 11:15

1 Answers1

0

I am not sure I got your question correctly.

If you want to use the filename to generate a date, you can simply parse it and pass it to datetime.datetime(). Remeber to pass the appropriate tzinfo information.

import datetime

fname = '20191112.txt'
year = int(d[:4])
month = int(d[4:6])
day = int(d[6:8])
date = datetime.datetime(year, month, day, datetime.tzinfo=timezone.utc)

Now date is a datetime object.

>>> date
datetime.datetime(2019, 11, 2, 0, 0)

You can convert it easily to a UNIX timestamp if that is what you need in your script.

>>> date.timestamp()
1572649200.0
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • If you use datetime objects, make them timezone (+ dst) aware. otherwise, `date.timestamp()` will not give you a utc timestamp and you won't have any info on the timezone. that can get you in all kinds of trouble later on... – FObersteiner Nov 12 '19 at 11:08
  • @MrFuppes excellent point. I have modified the answer to include that. – alec_djinn Nov 12 '19 at 11:47