Effortlessly Increment Your File Names with Python (Free Code)

How do you create a file name with an incrementing number in Python? To create an incrementing file name in Python, run a while loop that checks the existence of a file, and if it exists, use Regex to modify the file name by replacing the integer at the end of the file with the next number. Continue looping until the file name does not exist in the destination folder. ...

September 2, 2022 · 2 min · 345 words · Ryan Sheehy

How To Format Time In 24-Hours Using Python (Examples)

How do you format time to be in 24-hours using Python? In Python you can format a datetime object to be displayed in 24-hour time using the format %H:%M. This is not to be confused with the 12-hour time which uses the format %I:%M and may have %p appended to the end to denote AM/PM. Here’s an example demonstrating the conversion of time from 12-hour format to a 24-hour format: ...

September 1, 2022 · 2 min · 375 words · Ryan Sheehy

How To Format HH:MM AM/PM Time To 24 Hours In Python (Examples)

How do you format a string in the format of HH:MM AM/PM to 24-hour time in Python? To change a string in the format HH:MM AM/PM to 24-hour time in Python use the datetime.datetime.strptime(original_string, format_string) function located in the datetime library. As the datetime class needs to be imported from the datetime library you need to make sure you include the reference twice, otherwise you may get strptime AttributeError errors. Here’s an example of how this works: ...

August 26, 2022 · 3 min · 628 words · Ryan Sheehy

Convert DD/MM/YY To Date In Python (Examples)

How do you convert a string in the format of DD/MM/YY to a date in Python? To convert a string in the format of DD/MM/YY to a date in Python import the datetime library and use the datetime.datetime.strptime(date_string, format_string) function (yes, that is not a typo the datetime library has a datetime class and it contains the strptime function). To make it easier to access the strptime() function it helps to import the datetime class when the datetime library is imported, like so: from datetime import datetime. ...

August 26, 2022 · 3 min · 568 words · Ryan Sheehy

How To Find The Difference Between Two Or More Sets In Python

How can you find the difference between two sets in Python? In Python, you can find the difference between two sets using the .difference() method. The set and frozenset objects contain a built-in method labelled difference() which helps to find the non-matching elements in the source object not found in the other sets passed in to the .difference(*others) parameter. Here is an example demonstrating how to find the difference between two sets using the Python REPL: ...

August 17, 2022 · 3 min · 509 words · Ryan Sheehy

Python Ceiling Function: What Is It And How Does It Work?

What is the ceiling function in Python, and how does it work? The ceiling function in Python is found in the math library and it’s method name is ceil() which takes a sole numeric parameter. To use the ceiling function in Python you will need to import the math library, like so: 1 2 3 4 5 >>> import math >>> math.ceil(1.6) 2 >>> math.ceil(-2.1) -2 As you can see from the above Python REPL code the output produces the greatest integer. ...

August 6, 2022 · 3 min · 463 words · Ryan Sheehy

Download Images Using Selenium Python With Full Code Examples

How do you download images using Selenium Python? Selenium provides a way to create a screenshot of your browser’s view using the .save_screenshot(file_name) method, but this will take a photo of the viewport – what if you just want to download the image as it is? Unfortunately, Selenium doesn’t have the capability of selecting menu items in your browser window, therefore you will need to install a library that can help select menu items to the browser. ...

July 19, 2022 · 6 min · 1109 words · Ryan Sheehy

Selenium: Download PDF From URL To Specific Folder - No Printing Or Special Keys (Python)

How do you download a PDF file when the URL opens up a PDF in your Chrome browser in Python without needing to print the page or use special key presses? And how can you set the location of the PDF? The trick to be able to download a PDF file using Selenium without the Chrome browser opening the PDF file within the browser window is to set the preferences of the browser to simply not open PDF’s automatically. ...

June 22, 2022 · 4 min · 845 words · Ryan Sheehy

Python: Sort List Of Tuples By First Element In 10 Seconds

How do you sort a list of tuples by the first element in each tuple in Python? To sort a list of tuples in Python use the .sort() list method if you want to modify the list to sort or the sorted() function if you want to generate a new list. Use the parameter key with either function and set the value of that parameter to a lambda expression that returns the required tuple you want to sort by. ...

June 16, 2022 · 2 min · 418 words · Ryan Sheehy

What Does AttributeError: Module datetime Has No Attribute strptime Mean?

What does AttributeError: module 'datetime' has no attribute 'strptime'mean and how can you easily fix it? When parsing a string and transforming that to a date you can make use of the function strptime in the datetime module. To use this function simply import datetime as you normally would with any module, and use the function as follows: 1 2 3 >>> import datetime >>> datetime.datetime.strptime("16/06/22", "%d/%m/%y") datetime.datetime(2022, 6, 16, 0, 0) Notice in the above code that the function strptime is found within the module datetime and class datetime, therefore, if your code just has import datetime at the top you will need to enter the module and class followed by the function. ...

June 16, 2022 · 2 min · 293 words · Ryan Sheehy