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 …
Tag: python
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. …
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…
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…
Python multi-dimensional array initialization without a loop
Is there a way in Python to initialize a multi-dimensional array / list without using a loop? Answer Sure there is a way or but it’s horrible and wasteful, so everyone uses loops (usually list comprehensions) or numpy
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 t…
How to check if a user is logged in (how to properly use user.is_authenticated)?
I am looking over this website but just can’t seem to figure out how to do this as it’s not working. I need to check if the current site user is logged in (authenticated), and am trying: despite being sure that the user is logged in, it returns just: I’m able to do other requests (from the f…
Getting HTTP GET arguments in Python
I’m trying to run an Icecast stream using a simple Python script to pick a random song from the list of songs on the server. I’m looking to add a voting/request interface, and my host allows use of python to serve webpages through CGI. However, I’m getting hung up on just how to get the GET …
Python Regular Expression Match All 5 Digit Numbers but None Larger
I’m attempting to string match 5-digit coupon codes spread throughout a HTML web page. For example, 53232, 21032, 40021 etc… I can handle the simpler case of any string of 5 digits with [0-9]{5}, though this also matches 6, 7, 8… n digit numbers. Can someone please suggest how I would modify…