Categories
Python Answers

How to tail a log file in Python?

Sometimes, we want to tail a log file in Python.

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

How to tail a log file in Python?

To tail a log file in Python, we can use the sh library.

To install it, we run

pip install sh

For instance, we write

from sh import tail

for line in tail("-f", "/var/log/some_log_file.log", _iter=True):
    print(line)

to call tail with the options for tail and the file we want to run tail for.

And we set _iter to True to return a generator that returns new content whenever the file is updated.

Then we use a for loop to loop through the lines and print them.

Conclusion

To tail a log file in Python, we can use the sh library.

Categories
Python Answers

How to find the mime type of a file in Python?

Sometimes, we want to find the mime type of a file in Python.

In this article, we’ll look at how to find the mime type of a file in Python.

How to find the mime type of a file in Python?

To find the mime type of a file in Python, we can use the magic library.

To install it, we run

pip install python-magic

We also have to install a few packages in the OS the script is running on.

In Ubuntu like distros we run

sudo apt-get install libmagic1

And on Windows, we run

pip install python-magic-bin

On OS X, we run brew install libmagic with Homebrew or port install file with macports.

Then we use it by writing

import magic

mime = magic.Magic(mime=True)
mime_type = mime.from_file("testdata/test.pdf")

to create a Magic object.

Then we call from_file on it with the path to our file to return its MIME type.

Conclusion

To find the mime type of a file in Python, we can use the magic library.

Categories
Python Answers

How to access multiple elements of list knowing their index with Python?

Sometimes, we want to access multiple elements of list knowing their index with Python.

In this article, we’ll look at how to access multiple elements of list knowing their index with Python.

How to access multiple elements of list knowing their index with Python?

To access multiple elements of list knowing their index with Python, we can use the itemgetter function.

For instance, we write

from operator import itemgetter 

a = [-2, 1, 5, 3, 8, 5, 6]
b = [1, 2, 5]
print(itemgetter(*b)(a))

to call itemgetter with the indexes of b as the arguments.

And then we call the returned function with a to return a tuple of the items in a with the indexes in b.

Conclusion

To access multiple elements of list knowing their index with Python, we can use the itemgetter function.

Categories
Python Answers

How to return two values from a function in Python?

Sometimes, we want to return two values from a function in Python.

In this article, we’ll look at how to return two values from a function in Python.

How to return two values from a function in Python?

To return two values from a function in Python, we can return a tuple.

For instance, we write

def select_choice():
    # ...
    return i, card

my_i, my_card = select_choice()

to create the select_choice function that returns the i and card variables after putting them in a tuple.

Then we call select_choice and unpack the values from the tuple by assigning them to variables separated by commas.

Conclusion

To return two values from a function in Python, we can return a tuple.

Categories
Python Answers

How to run Python Selenium in Xvfb?

Sometimes, we want to run Python Selenium in Xvfb.

In this article, we’ll look at how to run Python Selenium in Xvfb.

How to run Python Selenium in Xvfb?

To run Python Selenium in Xvfb, we can use pyvirtualdisplay.

To install it, we run

pip install PyVirtualDisplay

Then we use it by writing

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.example.com')
print(browser.title)
browser.quit()

display.stop()

to create the Display and call start on it to start it.

Then we start the Firefox driver with

browser = webdriver.Firefox()

Then we open the webpage we want with get.

And then we get the title of the browser window with browser.window.

Finally, we call browser.quit to exit the browser and display.stop to stop the display.

Conclusion

To run Python Selenium in Xvfb, we can use pyvirtualdisplay.