Categories
Python Answers

How to get a directory listing sorted by creation date in Python?

Sometimes, we want to get a directory listing sorted by creation date in Python.

In this article, we’ll look at how to get a directory listing sorted by creation date in Python.

How to get a directory listing sorted by creation date in Python?

To get a directory listing sorted by creation date in Python, we can use the glob function.

For instance, we write

import glob
import os

search_dir = "/mydir/"
files = list(filter(os.path.isfile, glob.glob(search_dir + "*")))
files.sort(key=lambda x: os.path.getmtime(x))

to get the files in the search_dir with glob.glob.

And we call filter to return an iterable with the glob results that are files with os.path.isfile.

Next, we convert the iterable to a list with list.

Then we sort the list by creation date by calling sort with the key argument set to a function that gets the modified date with os.path.getmtime(x) and sort by the returned value.

Conclusion

To get a directory listing sorted by creation date in Python, we can use the glob function.

Categories
Python Answers

How to download a picture via urllib and Python?

Sometimes, we want to download a picture via urllib and Python.

In this article, we’ll look at how to download a picture via urllib and Python.

How to download a picture via urllib and Python?

To download a picture via urllib and Python, we can use the urllib.request.urlretrieve method.

For instance, we write

import urllib.request

urllib.request.urlretrieve("https://picsum.photos/200/300", "pic.jpg")

to call urllib.request.urlretrieve with the URL of the image and the file name to save it as.

Conclusion

To download a picture via urllib and Python, we can use the urllib.request.urlretrieve method.

Categories
Python Answers

How to send User-agent using Requests library in Python?

Sometimes, we want to send User-agent using Requests library in Python.

In this article, we’ll look at how to send User-agent using Requests library in Python.

How to send User-agent using Requests library in Python?

To send User-agent using Requests library in Python, we can add the User-Agent request header is the headers.

For instance, we write

import requests

url = "https://example.com"

headers = {
    "User-Agent": "My User Agent 1.0",
}

response = requests.get(url, headers=headers)

to create the headers dict that has the User-Agent request header key and value.

Then we call requests.get with the url and the headers to make a GET request to the url with the user agent header.

Conclusion

To send User-agent using Requests library in Python, we can add the User-Agent request header is the headers.

Categories
Python Answers

How to convert Python datetime to epoch with strftime?

Sometimes, we want to convert Python datetime to epoch with strftime.

In this article, we’ll look at how to convert Python datetime to epoch with strftime.

How to convert Python datetime to epoch with strftime?

To convert Python datetime to epoch with strftime, we use the timestamp method

For instance, we write

ts = datetime.datetime(2022, 4, 1, 0, 0).timestamp()

to create a datetime with datetime.datetime.

And then we call timestamp to return the Unix timestamp in seconds.

Conclusion

To convert Python datetime to epoch with strftime, we use the timestamp method

Categories
Python Answers

How to specify multiple return types using type-hints with Python?

Sometimes, we want to specify multiple return types using type-hints with Python.

In this article, we’ll look at how to specify multiple return types using type-hints with Python.

How to specify multiple return types using type-hints with Python?

To specify multiple return types using type-hints with Python, we can use Union.

For instance, we write

from typing import Union


def foo(client_id: str) -> Union[list, bool]
   # ...

to create the foo function that takes the client_id string parameter and returns either a list or a boolean as specified by

Union[list, bool]

Conclusion

To specify multiple return types using type-hints with Python, we can use Union.