Categories
Python Answers

How to count the number of occurrences of a character in a string with Python?

Sometimes, we want to count the number of occurrences of a character in a string with Python.

In this article, we’ll look at how to count the number of occurrences of a character in a string with Python.

How to count the number of occurrences of a character in a string with Python?

To count the number of occurrences of a character in a string with Python, we can use the string count method.

For instance, we write

c = 'Mary had a little lamb'.count('a')

to count the number of occurrences of 'a' in the 'Mary had a little lamb' string.

Conclusion

To count the number of occurrences of a character in a string with Python, we can use the string count method.

Categories
Python Answers

How to move a file in Python?

Sometimes, we want to move a file in Python.

In this article, we’ll look at how to move a file in Python.

How to move a file in Python?

To move a file in Python, we can use os.rename, os.replace or shutil.move.

For instance, we write

import os
import shutil

os.rename("path/to/current/file.foo", "path/to/destination/for/file.foo")
os.replace("path/to/current/file.foo", "path/to/destination/for/file.foo")
shutil.move("path/to/current/file.foo", "path/to/destination/for/file.foo")

to call os.rename, os.replace, and shutil.move with the source and destination paths.

We move the file from the source path to the destination path with all 3 methods.

Conclusion

To move a file in Python, we can use os.rename, os.replace or shutil.move.

Categories
Python Answers

How to count string with overlapping occurrences with Python?

Sometimes, we want to count string with overlapping occurrences with Python.

In this article, we’ll look at how to count string with overlapping occurrences with Python.

How to count string with overlapping occurrences with Python?

To count string with overlapping occurrences with Python, we can use the string find method.

For instance, we write

def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count += 1
        else:
            return count

to create the occurrences function that finds the instances of the sub substring that from index start with find.

If start is bigger than 0, then there’s one instance of overlap, so we increment count by 1.

Once we stop finding overlaps, we return count.

Conclusion

To count string with overlapping occurrences with Python, we can use the string find method.

Categories
Python Answers

How to use module ‘subprocess’ with timeout with Python?

Sometimes, we want to use module ‘subprocess’ with timeout with Python.

In this article, we’ll look at how to use module ‘subprocess’ with timeout with Python.

How to use module ‘subprocess’ with timeout with Python?

To use module ‘subprocess’ with timeout with Python, we can use the check_output function with the timeout argument.

For instance, we write

from subprocess import STDOUT, check_output

output = check_output(cmd, stderr=STDOUT, timeout=seconds)

to call check_output to run the cmd command with the timeout given in seconds.

We set stderr to STDOUT to output errors to stdout.

Conclusion

To use module ‘subprocess’ with timeout with Python, we can use the check_output function with the timeout argument.

Categories
Python Answers

How to save an object to disk with Python?

Sometimes, we want to save an object to disk with Python.

In this article, we’ll look at how to save an object to disk with Python.

How to save an object to disk with Python?

To save an object to disk with Python, we can use the dill.dump method.

For instance, we write

import dill

dill.dump(company1, file = open("company1.pickle", "wb"))

company1_reloaded = dill.load(open("company1.pickle", "rb"))

to call dill.dumnp to save the company1 object to company1.pickle.

And then we load the company1.pickle file into an object with dill.load.

We install dill by running

pip install dill

Conclusion

To save an object to disk with Python, we can use the dill.dump method.