[Tutor] operate on files based on comparing filenames to today's date
I have cobbled together this code which gives me regex matches of files whose names either begin with either MMDD_? or erased_YYMMDD_? and whose extensions are exml or ewav. Now I need to compare the date string (regex match) in the filename to today's date. If the result of the comparison results in YYYMMDD being older than 180 days, I should print something to indicate this. If not, nothing need be done. Should I write another function to compare each matched regex to today's date or is that overkill? Thanks. todaystring = date.today().strftime("%Y%m%d") oldest = date.today() - timedelta(days=180) def get_files(extensions): all_files = [] for ext in extensions: all_files.extend(Path('/Users/msh/Python/calls').glob(ext)) return all_files for file in get_files(('*.ewav', '*.exml')): print(re.match("[0-9]{8}|erased_",file.name)) -- The plural of anecdote is not "data." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] operate on files based on comparing filenames to today's date
Before getting to your specific question, a few remarks below: On 28Mar2019 12:08, Matthew Herzog wrote: I have cobbled together this code which gives me regex matches of files whose names either begin with either MMDD_? or erased_YYMMDD_? and whose extensions are exml or ewav. todaystring = date.today().strftime("%Y%m%d") oldest = date.today() - timedelta(days=180) def get_files(extensions): all_files = [] for ext in extensions: all_files.extend(Path('/Users/msh/Python/calls').glob(ext)) return all_files for file in get_files(('*.ewav', '*.exml')): print(re.match("[0-9]{8}|erased_",file.name)) Your regexp in the "print" loop at the bottom does not do what you say. You have: print(re.match("[0-9]{8}|erased_",file.name)) i.e. the regexp is: [0-9]{8}|erased_ (a) That matches 8 digits _or_ the string "erased_". (b) [0-9] can be written as \d for more readability. (c) I'd use: (erased_)?\d{8} which is an optional "erased_" followed by 8 digits. And for your purposes: (erased_)?(\d{8}) which will group the digits together for easy extraction: DATED_FILENAME_re = re.compile(r'(erased_)?(\d{8})') for file in get_files(('*.ewav', '*.exml')): m = DATED_FILENAME_re.match(file) if m: # a suitable filename datepart = m.group(2) # now you can turn that into an actual datetime.date object Now I need to compare the date string (regex match) in the filename to today's date. If the result of the comparison results in YYYMMDD being older than 180 days, I should print something to indicate this. If not, nothing need be done. Should I write another function to compare each matched regex to today's date or is that overkill? Thanks. If you want to figure out the age, you need to convert the MMDD into a datetime.date and then compare _that_ to today's date. Why? Because the datetime.date type knows how to work with dates correctly, avoiding all sorts of bug prone efforts on your own part (because human calendars are irregular tricky things with many special cases). So I would drop the "todaystring" altogether. You're thinking "get the string from the filename and compare it to "todaystring". But what you _want_ it to measure the age, and that requires working with dates, not strings. So instead convert the filename's string in to a date and do straight arithmetic with today(). So you might upgrade that regexp to group the year, month and day individually, pull them out and make a date object: import datetime DATED_FILENAME_re = re.compile(r'(erased_)?(\d\d\d\d)(\d\d)(\d\d)') # get today as a datetime.date object today = datetime.today() for file in ..: m = DATED_FILENAME_re.match(file) if m: prefix, year, month, day = m.group(1,2,3,4) year = int(year) month = int(month) day = int(day) # turn this into a datetime.date object date = datetime.date(year, month, day) # compute a datetime.timedelta object age = today - date if age.days > 180: print("old!") I think you're right: make a function to return the datetime.date of the filename, so this portion: m = DATED_FILENAME_re.match(file) if m: prefix, year, month, day = m.group(1,2,3,4) year = int(year) month = int(month) day = int(day) # turn this into a datetime.date object date = datetime.date(year, month, day) and return the computed date. Then just keep this in the for loop so it is obvious what's going on; it is too small to hide in a function without a better reason. # compute a datetime.timedelta object age = today - date if age.days > 180: print("old!") Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with code
I’m trying to make a password that must contain a number in it. Which method do I use for that? Sent from my iPhone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with code
On 28/03/2019 21:12, lucasbreault2...@gmail.com wrote: > I’m trying to make a password that must contain a number in it. I assume you mean you want to check whether a password has a number in it? Does it need to be a single digit or can there be multiple? > Which method do I use for that? There is a string method called isdigit() that will test for a number. You can loop over each character in the password and apply that method. This smells a bit like homework so I won't go any further than that, but if you get stuck send us your code and we'll try to help some more. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] cant get it to run right
I am new and I have tried everything I can think I of. I want it to run like a converstion. print(" I am Bob") name = input("What is your name") print("Nice to meet you") age = input("How old are you?") print("I was not programed with a age") city = input("Where do you live?") temperature = float(input("What is the temperature?")) if temperature>=75: print("WOW it is hot") elif temperature==75 or 74 or 73 or 72 or 71 or 70 or 69 or 68 or 67 or 66 or 65: print(" That is nice") else: print(" You can't make me go outside") color = input("What is your favorite color") if color=="Red" or "red" or "yellow" or "Yellow" or "Blue" or "blue" or "orange" or "Orange" or "green" or "Green" or "purple" or "Purple": print ("That is cool") elif color=="white" or "White": input(" That is a shade, Try agian") elif color=="gray" or "Gray": input(" That is a shade, Try agian") elif color=="black" or "Black": input("That is not a color,Try agian") else: print (" I dont know what to say") print("My favorite color is Green") maxNumber=int(input("Pick a number between 1 and 10")) count=0 while counthttps://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cant get it to run right
On 29/03/19 12:55 PM, Sveum, Christian wrote: I am new and I have tried everything I can think I of. I want it to run like a converstion. print(" I am Bob") ... What is not working? How far does the program run before stopping? What error message are you seeing? (why won't you share it with us?) -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cant get it to run right
On 28/03/2019 23:55, Sveum, Christian wrote: > I am new and I have tried everything I can think I of. I want it to run like > a converstion. Its always good to be as specific as possible. What exactly do you mean by "can't get it to run right"? Does it crash? Do you get an error message? If so send the message to us. Does it give the "wrong answer" If so tell us what you expected and what you got. > print(" I am Bob") > name = input("What is your name") > print("Nice to meet you") > age = input("How old are you?") > print("I was not programed with a age") > city = input("Where do you live?") You save all these values but don't use them. > temperature = float(input("What is the temperature?")) > if temperature>=75: > print("WOW it is hot") > elif temperature==75 or 74 or 73 or 72 or 71 or 70 or 69 or 68 or 67 or 66 or > 65: > print(" That is nice") Now this might be where you hit problems. The code above is valid but doesn't do what I suspect you think it does. Python sees it as: elif (temperature==75) or (74 or 73 or 72 or 71 or 70 or 69 or 68 or 67 or 66 or 65): And the expression (75 or 74 0r...) always evaluates to the first non-zero value, so in this case it's 74 But we know temperature is not equal to 75 (from the if test) so the elif expression always evaluates to 74 which is True and so you get the printed message. To do what you want you have several options. In your case the values form a range so you can test that: elif 75 > temperature > 65:# between 65 and 75 print... Or you can put your values in a list and use 'in': elif temperature in [75,74,73,72,71,70,69,68,67,66,65]: print The range option is vest if your user can type in non-whole numbers like 72.5 (as the use of float suggests) The list is best if you have a set of distinct but non consecutive values) > if color=="Red" or "red" or "yellow" or "Yellow" or "Blue" or "blue" or "orange" or "Orange" or "green" or "Green" or "purple" or "Purple": Same here but the list approach is the only option here. But see below... > elif color=="white" or "White": in this case the best approach is to convert the strings to upper or lower case: elif color.lower() == "white": > elif color=="gray" or "Gray": > elif color=="black" or "Black": Same here > while count if count==10: > print( "10 is my favorite number!") > print(count) > count=count+1 > input("Was one of these numbers your favorite number") > if question=="yes": I suspect you meant to assign the input() value to question? HTH -- -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor