I have a folder called C:\Flows in which I have multiple files in the format of 20190101_Flows.csv for all the days of this year.
I want to import a file for each day perform operations on it and then save the file. However, I'm stuck at it.
So far, I have written the code below to get the files. But I am stuck after that.
from datetime import date
from datetime import datetime, timedelta
import glob
from os import path
DATE_FORMAT = "%Y%m%d"
pathDir = r'C:/Flows/'
dateStart = "20190301"
dateEnd = "20190305"
start_date = datetime.strptime(dateStart, DATE_FORMAT).date()
end_date = datetime.strptime(dateEnd, DATE_FORMAT).date()
delta_one_day = timedelta(days=1)
date = start_date
while date <= end_date:
data_folder = path.join(pathDir)
if path.isdir(data_folder):
for filename in glob(os.path.join(data_folder, "*_Flows.csv")):
#this is where i would like to perform the operations on the file imported
df['code']=df[df.columns[1]].astype(str).str[0:3]
df['bmunit']=df[df.columns[1]].astype(str).str[4:]
df['checko']=df.iloc[:,2:50].sum(axis=1)
print filename
date += delta_one_day
Related
I've been trying to create python program where each time it runs it should create a folder in a specific path with the today's date using the format MMDDYY but with little success.
Here's the code I've tried:
import os
from datetime import datetime, timedelta
def next_date(date, period):
new_date = date + timedelta(days=period)
return '/'.join((str(new_date.day), str(new_date.month), str(new_date.year))), new_date
def make_directories(home_dir, start_date, instances, period):
date = datetime.strptime(start_date, "%m/%d/%y")
for i in range(instances):
start_date = start_date.replace('/', '')
dirpath = os.path.join(directory, start_date)
try:
os.makedirs(dirpath)
except FileExistsError:
print('Directory {} already exists'.format(dirpath))
else:
print('Directory {} created'.format(dirpath))
start_date, date = next_date(date, period)
if __name__ == "__main__":
directory = "\PROJECTS\PTV\testing\2023_test"
I'd like to move files (not copy) base on range of file time modification.
I try to find solution and I found code as below.
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")
This code is work when I put specific date modification require but I want to move file base on range of file modification time.
for example : If create modification time 00:00 - 12:00 move file to a folder1
and 13:00 - 24:00 move to folder2 by do not necessary input range of time.
Please supporting if you have any idea.
Use datetime.time instead if you want use time of day. To convert the "Unix" time from os.path.getmtime() to a time object, I think its easier to first make a datetime. I didn't include any input reading since I wasn't sure how you want to deal with it:
import datetime
import os
def created_in_range(created, start_hours, end_hours):
created_time = datetime.datetime.fromtimestamp(created).time()
return datetime.time(hour=start_hours) < created_time < datetime.time(hour=end_hours)
src = "."
for filename in os.listdir(src):
if not os.path.isfile(filename):
continue
created = os.path.getmtime(os.path.join(src, filename))
if created_in_range(created, 0, 12):
print("Move to folder_1")
elif not created_in_range(created, 0, 13):
print("Move to folder_2")
I had to use a not keyword for 1300 - 2400 range since the "hour" argument for time needs to be in between 0 and 23.
I need to keep the backup file started on 31st april and ended next day.
how to get last day backup files .
backup file names are identical except the timestamp and it starts like 221information_schema202205052355
import os
import time
import datetime
def main():
start_date = datetime.datetime.strptime('2022-03-01', '%Y-%m-%d')
to_delete = 'D:\S135'
if os.path.exists(to_delete):
if os.path.isdir(to_delete):
for root_folder, folders, files in os.walk(to_delete):
for file in files:
curpath = os.path.join(to_delete, file)
file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
if datetime.datetime.now() - file_modified > datetime.timedelta(days=14):
if file_modified > start_date :
print(curpath)
if __name__ == "__main__":
# invoking main function
main()
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!
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
This is my example path: 'c:\Data\2015-08-01'
Currently I'm getting all the files inside on one(1) specific date, but my goal is to get the files with date range of file folder. Example is to get 2015-08-01 to 2015-08-05' just like the BETWEEN query in MySQL
import os
import os.path
import tempfile
dateStart = '2015-08-01'
dateEnd = '2015-08-05'
year = dateStart[0:4]
yearMonth = year + '_' + dateStart[5:7]
pathDir = 'c:\\Data'
date_folder = pathDir + '\\' + dateStart
count = 0
for filefolder in os.listdir(date_folder):
filefolder = date_folder + "\\" + filefolder
for file in os.listdir(filefolder):
if "txt" in file:
filename = filefolder + "\\" + file
print filename
#Output of this, is all text files for this date only '2015-08-01'
Its hard for me to loop to pull files for date range e.g. '2015-08-01' to '2015-08-05'. How to do this?
Note that there is a folder after my dates and the textfiles are in the last. and the textfile containing on that folder is my point to get. so that from my old code I used this: filefolder = date_folder + "\" + filefolder to get the text in 1 date only.
Here is my sample real path data:
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-01\Folder\data.text
and if I will get the range from 2015-08-01 to 2015-08-01. this will be the output:
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-01\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-02\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-03\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-04\Folder\data.text
\\10.81.67.162\DLCx Logs\DLCx02\2015\2015_08\2015-08-05\Folder\data.text
Here is my approach: start with separate year, month, day and build the date:
import glob
import os
pattern = os.path.join(r'C:\Data', '{}-{:02}-{:02}', '*', '*.txt')
year, month = 2015, 8
start_day, end_day = 1, 5
for day in range(start_day, end_day + 1):
wildcard = pattern.format(year, month, day)
for filename in glob.glob(wildcard):
print filename
The datetime module makes doing date arithmetic, comparisons, as well as converting them to or from strings relatively easy.
Here's how it could be used to do what you're trying to accomplish (at least according to your most recent comments):
from datetime import datetime, timedelta
from glob import glob
from os import path
DATE_FORMAT = '%Y-%m-%d'
SUBFOLDER_PATH_FORMAT = r'%Y\%Y_%m\%Y-%m-%d\Folder'
pathDir = r'\\10.81.67.162\DLCx Logs\DLCx02'
dateStart = '2015-08-01'
dateEnd = '2015-09-01'
start_date = datetime.strptime(dateStart, DATE_FORMAT).date()
end_date = datetime.strptime(dateEnd, DATE_FORMAT).date()
delta_one_day = timedelta(days=1)
date = start_date
while date <= end_date:
subfolder_path = date.strftime(SUBFOLDER_PATH_FORMAT)
data_folder = path.join(pathDir, subfolder_path)
if path.isdir(data_folder):
for filename in glob(os.path.join(data_folder, '*.txt')):
print filename
date += delta_one_day
It is easiest to convert your dates to date objects. You can then just compare them. See example below:
#!/usr/bin/python
import os
import os.path
import tempfile
import datetime
import re
dateStart = '2015-08-03'
dateEnd = '2015-08-05'
# helper function to convert date strings to date objects
def make_dt(ds):
return datetime.date(int(ds[0:4]), int(ds[5:7]), int(ds[8:10]))
# convert date string to date object
dt_start = make_dt(dateStart)
dt_end = make_dt(dateEnd)
pathDir = '.'
if __name__ == "__main__":
for folder in os.listdir(pathDir):
# only folders that match date format yyyy-mm-dd
if re.match("[0-9]{4}-[0-9]{2}-[0-9]{2}", folder):
# convert folder name to date object
dt_folder = make_dt(folder)
if (dt_folder <= dt_end) and (dt_folder >= dt_start):
print "folder %s is between start [%s] and end [%s]" % (folder, dateStart, dateEnd)