Categories
Python Answers

How to convert a datetime object to seconds with Python?

Sometimes, we want to convert a datetime object to seconds with Python.

In this article, we’ll look at how to convert a datetime object to seconds with Python.

How to convert a datetime object to seconds with Python?

To convert a datetime object to seconds with Python, we can use the datetime timestamp method.

For instance, we write

from datetime import datetime

dt = datetime.today()
seconds = dt.timestamp()

to get today’s datetime with datetime.today.

Then we call timestamp on the returned datetime object to get the timestamp of it in seconds.

Conclusion

To convert a datetime object to seconds with Python, we can use the datetime timestamp method.

Categories
Python Answers

How to check whether a string starts with XXXX with Python?

Sometimes, we want to check whether a string starts with XXXX with Python.

In this article, we’ll look at how to check whether a string starts with XXXX with Python.

How to check whether a string starts with XXXX with Python?

To check whether a string starts with XXXX with Python, we can use the startswith method.

For instance, we write

a_string = "hello world"
a_string.startswith("hello")

to call a_string.startswith to check if a_string starts with 'hello'.

Conclusion

To check whether a string starts with XXXX with Python, we can use the startswith method.

Categories
Python Answers

How to convert NumPy array into Python List structure?

Sometimes, we want to convert NumPy array into Python List structure.

In this article, we’ll look at how to convert NumPy array into Python List structure.

How to convert NumPy array into Python List structure?

To convert NumPy array into Python List structure, we can use the tolist method.

For instance, we write

import numpy as np
l = np.array([[1,2,3],[4,5,6]]).tolist()

to create an NumPy array with np.array.

Then we call tolist on the NumPy array to return a list with the items that we create the array with.

Conclusion

To convert NumPy array into Python List structure, we can use the tolist method.

Categories
Python Answers

How to capture stdout output from a Python function call?

Sometimes, we want to capture stdout output from a Python function call.

In this article, we’ll look at how to capture stdout output from a Python function call.

How to capture stdout output from a Python function call?

To capture stdout output from a Python function call, we can use the redirect_stdout function.

For instance, we write

import io
from contextlib import redirect_stdout

f = io.StringIO()
with redirect_stdout(f):
    do_something(my_object)
out = f.getvalue()

to call redirect_stdout with the f StringIO object.

Then we call do_something which prints stuff to stdout.

And then we get the value printed to stdout with f.getvalue.

Conclusion

To capture stdout output from a Python function call, we can use the redirect_stdout function.

Categories
Python Answers

How to do a recursive sub-folder search and return files in a list with Python?

Sometimes, we want to do a recursive sub-folder search and return files in a list with Python.

In this article, we’ll look at how to do a recursive sub-folder search and return files in a list with Python.

How to do a recursive sub-folder search and return files in a list with Python?

To do a recursive sub-folder search and return files in a list with Python, we can use glob.

For instance, we write

import os
from glob import glob

result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.txt'))]

to call os.walk to get the directories in PATH.

And then we loop through then files in y to get the files with the txt extension in the directory with

glob(os.path.join(x[0], '*.txt')

os.walk will recursively traverse child directories in PATH.

Conclusion

To do a recursive sub-folder search and return files in a list with Python, we can use glob.