Python Forum

Full Version: need to define date range of files to copy over
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

This is my first post so really sorry if I have done this incorrectly.

I have added raw_input that lets the user choose file extension.
The next criteria I am looking to add is a date range so they can choose a date range for example:

17/07/2020 to 04/08/2020 or Today() like in excel

import os
import shutil
import os.path, time
from pip._vendor.distlib.compat import raw_input

os.chdir('C://')
src = ("")
dst = ("")
ext = raw_input("[+] File format: ")
created = (" last modified: %s" % time.ctime(os.path.getmtime(src)))
start = raw_input("[+] Date start: ")
end = raw_input("[+] Date end: ")

for filename in os.listdir(src):
    if filename.endswith('.'+ext) and created.startswith(start) and created.endswith(end):
        shutil.copy( src + filename, dst)
    print("[+] File transferred "+filename + created)
example on terminal
file extension = .csv
startdate = 12/05/2020
enddate = 07/08/2020

once the user has input these fields it would copy only the required files over.

The current output of the created files are:

[+] File transferred BASE1011.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1112.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1213.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1314.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1415.xls last modified: Fri Jul 17 10:11:40 2020

I want these to be in an easier format for user input as explained above:

example: start 12/05/2020 end date = 07/08/2020

Thank you for your help, I am not the best at python so really sorry about poor coding but I am trying to learn so any help would be amazing.

Thanks
solved my own issue thanks
why do you import raw_input from pip._vendor.distlib.compat? You should be using python3 and input to take user input.
Thank you I have changed the code so it is only using input, I just read up about raw_input and been using it for a couple of weeks, will need to go through all my codes and replace it.

here is the code I have.

import os
import shutil
import time
from datetime import datetime

src = "C:/Users/eldri/OneDrive/Desktop"
dst = "C:/Users/eldri/OneDrive/Desktop/output"
ext = input("[+] File format: ")  # "txt"
start = input("[+] Date start: ")  # "01/07/2020"
end = input("[+] Date end: ")  # "30/07/2020"


def dateRange(createdDate, startDate, endDate):
    """determines if date is in range"""
    createdDate = datetime.strptime(createdDate, '%a %b %d %H:%M:%S %Y')
    startDate = datetime.strptime(startDate, '%d/%m/%Y')
    endDate = datetime.strptime(endDate, '%d/%m/%Y')
    return startDate < createdDate < endDate


for filename in os.listdir(src):
    created = time.ctime(os.path.getmtime(src + filename))
    if filename.endswith('.' + ext) and dateRange(created, start, end):
        shutil.copy(src + filename, dst)
        print("[+] File transferred " + filename + created)
    else:
        print("[+] File not transferred " + filename + created)

print("[+] Transfer complete")
Can i ask whether it is possible to add a progress bar to my code? if so how would I go about it?
tqdm package
example by @ snippsat: https://python-forum.io/Thread-tqdm-Prog...mmand-line

progressbar2 package

There are other options too (e.g. click.progressbar, part of click - but this will be overkill if not using click for CLI interface).