“SyntaxError: Non-ASCII character …” or “SyntaxError: Non-UTF-8 code starting with …” trying to use non-ASCII text in a Python script

I’d recommend reading that PEP the error gives you. The problem is that your code is trying to use the ASCII encoding, but the pound symbol is not an ASCII character. Try using UTF-8 encoding. You can start by putting # -*- coding: utf-8 -*- at the top of your .py file. To get more …

Read more

One try block with multiple excepts

Yes, it is possible. try: … except FirstException: handle_first_one() except SecondException: handle_second_one() except (ThirdException, FourthException, FifthException) as e: handle_either_of_3rd_4th_or_5th() except Exception: handle_all_other_exceptions() See: http://docs.python.org/tutorial/errors.html The “as” keyword is used to assign the error to a variable so that the error can be investigated more thoroughly later on in the code. Also note that the parentheses …

Read more

How to call a script from another script?

The usual way to do this is something like the following. test1.py def some_func(): print ‘in test 1, unproductive’ if __name__ == ‘__main__’: # test1.py executed as script # do something some_func() service.py import test1 def service_func(): print ‘service func’ if __name__ == ‘__main__’: # service.py executed as script # do something service_func() test1.some_func()

How do I check if a directory exists in Python?

Use os.path.isdir for directories only: >>> import os >>> os.path.isdir(‘new_folder’) True Use os.path.exists for both files and directories: >>> import os >>> os.path.exists(os.path.join(os.getcwd(), ‘new_folder’, ‘file.txt’)) False Alternatively, you can use pathlib: >>> from pathlib import Path >>> Path(‘new_folder’).is_dir() True >>> (Path.cwd() / ‘new_folder”https://stackoverflow.com/”file.txt’).exists() False

Get meta tag content property with BeautifulSoup and Python

Provide the meta tag name as the first argument to find(). Then, use keyword arguments to check the specific attributes: title = soup.find(“meta”, property=”og:title”) url = soup.find(“meta”, property=”og:url”) print(title[“content”] if title else “No meta title given”) print(url[“content”] if url else “No meta url given”) The if/else checks here would be optional if you know that …

Read more

Django : Can’t import ‘module’. Check that module AppConfig.name is correct

According to the documentation, AppConfig.name is a full python path to the application. AppConfig.name Full Python path to the application, e.g. ‘django.contrib.admin’. This attribute defines which application the configuration applies to. It must be set in all AppConfig subclasses. It must be unique across a Django project. https://docs.djangoproject.com/en/2.2/ref/applications/#django.apps.AppConfig.name Try this: class CoreConfig(AppConfig): name=”compfactu.core”