Skip to content
Advertisement

Titlecasing a string with exceptions

Is there a standard way in Python to titlecase a string (i.e. words start with uppercase characters, all remaining cased characters have lowercase) but leaving articles like and, in, and of lowercased? Answer There are a few problems with this. If you use split and join, some white space characters will be ignored. The built-in capitalize and title methods do

How to get the text cursor position in Windows?

Is it possible to get the overall cursor position in Windows using the standard Python libraries? Answer This retrieves the cursor’s position, in screen coordinates – point = (x,y) Retrieves information about the global cursor. Links: http://msdn.microsoft.com/en-us/library/ms648389(VS.85).aspx http://msdn.microsoft.com/en-us/library/ms648390(VS.85).aspx I am assuming that you would be using python win32 API bindings or pywin32.

How to know the directory where the python script is called?

Let’s say that I have a python script a.py in /A/B/a.py, and it’s in the PATH environment variable. The current working directory is /X/Y/, and it’s the directory where I call the /A/B/a.py. In a.py, how to detect /X/Y/? I mean, how to know in which directory the python call is made? Answer You can get the current working directory

Global static variables in Python

I need to use list data in other functions, but I don’t want to enter raw_input everytime. How I can make data like a global static in c++ and put it everywhere where it needed? Answer Add the global keyword to your function: The global data statement is a declaration that makes data a global variable. After calling Input() you

How to write a multidimensional array to a text file?

In another question, other users offered some help if I could supply the array I was having trouble with. However, I even fail at a basic I/O task, such as writing an array to a file. Can anyone explain what kind of loop I would need to write a 4x11x14 numpy array to file? This array consist of four 11

A weighted version of random.choice

I needed to write a weighted version of random.choice (each element in the list has a different probability for being selected). This is what I came up with: This function seems overly complex to me, and ugly. I’m hoping everyone here can offer some suggestions on improving it or alternate ways of doing this. Efficiency isn’t as important to me

Using Python, getting the name of files in a zip archive

I have several very large zip files available to download on a website. I am using Flask microframework (based on Werkzeug) which uses Python. Is there a way to show the contents of a zip file (i.e. file and folder names) – to someone on a webpage – without actually downloading it? As in doing the working out server side.

Python ‘source HOME/.bashrc’ with os.system()

I am writing a python script (Linux) that is adding some shell aliases (writes them to HOME/.bash_aliases). In order to make an alias available immediately after it was written I should issue the following bash built-in: source is a bash built-in so I cannot just: If i try something like: …will freeze the script (just like is waiting for something).

Advertisement