Categories
Python Answers

How to fix PermissionError: [Errno 13] Permission denied with Python open?

To fix PermissionError: [Errno 13] Permission denied with Python open, we should make sure the path we call open with is a file.

For instance, we write

import os

path = r"/path/to/file.txt"
assert os.path.isfile(path)
with open(path, "r") as f:
    pass

to make sure that the path is a path to a file with os.path.isfile before we call open to open the file at the path.

Categories
Python Answers

How to load default profile in Chrome using Python Selenium Webdriver?

Sometimes, we want to load default profile in Chrome using Python Selenium Webdriver.

In this article, we’ll look at how to load default profile in Chrome using Python Selenium Webdriver.

How to load default profile in Chrome using Python Selenium Webdriver?

To load default profile in Chrome using Python Selenium Webdriver, we can use the ChromeOptions object.

For instance, we write

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path")
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

to create a ChromeOptions object.

And then we call add_argument to set some options for Chrome.

Next, we create a Chrome object with the executable_path of the Chrome driver and the options ChromeOptions object to set the options when running Chrome.

Conclusion

To load default profile in Chrome using Python Selenium Webdriver, we can use the ChromeOptions object.

Categories
Python Answers

How to apply function to each element of a list with Python?

Sometimes, we want to apply function to each element of a list with Python.

In this article, we’ll look at how to apply function to each element of a list with Python.

How to apply function to each element of a list with Python?

To apply function to each element of a list with Python, we use the map function.

For instance, we write

from string import upper

mylis = ['this is test', 'another test']
mapped = map(upper, mylis)

to create the mylis with some strings inside.

Then we call map to call the upper function on each entry of mylis.

An iterator is then returned with the mylis string in upper case from map.

Conclusion

To apply function to each element of a list with Python, we use the map function.

Categories
Python Answers

How to get text with Selenium WebDriver in Python?

Sometimes, we want to get text with Selenium WebDriver in Python.

In this article, we’ll look at how to get text with Selenium WebDriver in Python.

How to get text with Selenium WebDriver in Python?

To get text with Selenium WebDriver in Python, we use the element’s text property.

For instance, we write

driver.find_element_by_class_name("ctsymbol").text

to call find_element_by_class_name to get the first element with class ctsymbol.

And then we use the text property of it to get the text of the element returned.

Conclusion

To get text with Selenium WebDriver in Python, we use the element’s text property.

Categories
Python Answers

How to truncate float values with Python?

Sometimes, we want to truncate float values with Python.

In this article, we’ll look at how to truncate float values with Python.

How to truncate float values with Python?

To truncate float values with Python, we can use the round function.

For instance, we write

n = round(1.923328437452, 3)

to call round to round 1.923328437452 to 3 decimal places.

Conclusion

To truncate float values with Python, we can use the round function.