When should I ever use file.read() or file.readlines()?

The short answer to your question is that each of these three methods of reading bits of a file have different use cases. As noted above, f.read() reads the file as an individual string, and so allows relatively easy file-wide manipulations, such as a file-wide regex search or substitution. f.readline() reads a single line of … Read more

ImportError: no module named win32api

I am using Python 2.7 and I want to use pywin32-214 on Windows 7. I installed pywin32-214 by using the msi installer. But when I import win32api in my Python script, it throws the error: What should I do? Can I use pywin32 api for Windows 7?

Check if an object exists

Since filter returns a QuerySet, you can use count to check how many results were returned. This is assuming you don’t actually need the results. After looking at the documentation though, it’s better to just call len on your filter if you are planning on using the results later, as you’ll only be making one sql query: A count() call … Read more

Unable to import tweepy module

Try to pip uninstall tweepy and then again pip install tweepy Make sure you don’t have several interpreters on your computer, if you have several, make sure that your pycharm(or any other editor you use) is configured with the same interpreter where you installed tweepy.

How to randomly get 0 or 1 every time?

Display matrix of 0s and 1s Write a function that displays an n-by-n matrix using the following header: Each element is 0 or 1, which is generated randomly. Write a test program that prompts the user to enter n and displays an n-by-n matrix. Here is a sample run: Here is my idea:

What does on_delete do on Django models?

This is the behaviour to adopt when the referenced object is deleted. It is not specific to Django; this is an SQL standard. Although Django has its own implementation on top of SQL. (1) There are seven possible actions to take when such event occurs: CASCADE: When the referenced object is deleted, also delete the objects that have … Read more