I am writing a script to grab files from a directory based on the week number in the filenames. I need to grab files with week N and week N+1 in the filenames.
I have the basics down, but am now struggling to figure out the rollover for new years (the file format uses the isocalendar standard). Isocalendars can have either 52 or 53 weeks in them, so I need a way to figure out if the year I'm in is a 52 or 53 week year so that I can then compare to the results of datetime.now().isocalendar()[1] and see if I need to set N+1 to 1.
Is there a built in python function for this?
Why not just use (datetime.now() + timedelta(days=7)).isocalendar()[1] rather than calculating it from the current week number at all?
Related
I have a directory with 7000 daily .tif files, spanning from 2002 to 2022. Each file has a respective indication of its timestamp, in the yyyy/mm/dd format, as: My_file_20020513.tif
I'm trying to select the file paths according to their respective month, i.e. all files from January, all files from February, and so on.
I thought using a simple wildcard showing my month of interest would solve the problem, such as:
january_files = glob.glob('My_folder/My_file_*01*.tif')
But this ended up selecting all files that have a number 01 on its string, such as all the first days of a month, and even October's days that starts with a 1.
Is there a way I can use glob for selecting the file paths for a specific month only?
Use ? to match a single character. Then you can use four ? to match any year.
january_files = glob.glob('My_folder/My_file_????01*.tif')
I have to write a program where I find how many days of the year equal 7.
Example:
1/3/21 = 1+3+2+1 = 7.
My first idea was to create a list for the days, and then somehow loop it through for each month (and save the dates in a new list), but I'm not sure that's the best way.
No need for any difficult code, this is a beginner assignment.
Thanks in advance!
I have a use case where I need to count the server failures for current week. I am counting by reading a file where all the dates and failures are given. But I need to Calculate weekly failures, So I thought of to get all the dates in current week and compare that with dates in file and hence count the failures. So the question is how can I get all the dates of current week ? Also, how can i check if any date comes in that week?
Can anyone please help ?
Using pandas:
df.loc[df["dates"].dt.week == week_number]
This simply gets all the rows where week is equal to the specified week (you can find out that week by trying a dummy value and using .dt.week).
print(date.today())
for x in range(7):
print(date.today() + timedelta(days=x))
I am trying to determine the amount of days between updates on a ticket in zendesk, excluding weekends. I have been able to determine the difference between updates, but it doesnt take into account weekends. I want to determine if the user has updated their ticket within 3 days, excluding weekends, then close the ticket if it has been over 3 days. This is what I have:
updated_time = updated_time[0:10:1]
time_now = str(datetime.datetime.now())
time_now = time_now[0:10:1]
updated_time = dateutil.parser.parse(updated_time)
time_now = dateutil.parser.parse(time_now)
date_to_close = datetime.timedelta(days=3)
please_close = time_now - updated_time
My problem is because it doesnt distinguish between weekdays and weekends, it will automatically close the ticket on Monday if no one has responded over the weekend, which I would not like to count. I've been looking at the isoweekday() but its not really giving me what I need. I was thinking about trying to assign the ticket to a variable and assigning it a number, then increase that number by days without an update, that way it would not count the specific date but rather the number of days in between, but not sure how to do this or if it would even work.
Does anyone have an idea how to count the difference between days while excluding weekends? Is this possible?
If you already know how many days (of any kind), that value must be equal to 7w+x (that is, w full weeks and x days of the remaining partial week). You know that there are 5 weekdays in every full week; that just leaves figuring out how many of the remaining x days are weekdays, which you can determine from how many days until the first weekend and comparing that to x.
Ok, after a lot of digging and reaching out to another python programmer I know, he pointed me to the numpy function busday_count(). This does the calculation that I need. The reference is: Python - Exclude weekends between two Dates
Thanks very much for everyone's answers!
I'm writing a Python script that is to run continuously, performing some of its functionality only if it determines that the current time is between two daily times. Specifically, the script is given two command line arguments that are both of the form HHMM, where HH is the daily starting hour and MM is minutes and UTC is assumed.
So, for example, the script might be told to run through the day (0900, 1700), or it might be told to run through the night (2200, 0700).
Given such arguments -- that specify times that can be in the current day and the following day -- is there some straightforward way (perhaps using the datetime module) for the script to know if the current time is between these two times?
Construct two datetime objects from your arguments + today's date.
If the "later" one is less then the earlier one then add one day to the later time. It should now be greater than the early time.
Then compare the current time to see if it is greater than the early time and less than the later time.
start and end can be represented as tuples:
start = (9, 0)
end = (17,0)
now = datetime.datetime.utcnow().timetuple()[3:5] # gives you (hour,min)
print start <= now <= end