How do I make my python script wait until the user presses any key? Answer In Python 3, use input(): In Python 2, use raw_input(): This only waits for the user to press enter though. On Windows/DOS, one might want to use msvcrt. The msvcrt module gives you access to a number of functions in the Microsoft Visu…
Tag: python
Mark data as sensitive in python
I need to store a user’s password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as “sensitive”, so it’s not saved anywhere by a debugger? Answer Edit I have made a so…
Shuffling a list of objects
How do I shuffle a list of objects? I tried random.shuffle: But it outputs: Answer random.shuffle should work. Here’s an example, where the objects are lists: Note that shuffle works in place, and returns None. More generally in Python, mutable objects can be passed into functions, and when a function m…
How to get string objects instead of Unicode from JSON
I’m using Python 2 to parse JSON from ASCII encoded text files. When loading these files with either json or simplejson, all my string values are cast to Unicode objects instead of string objects. The problem is, I have to use the data with some libraries that only accept string objects. I can’t c…
How do I use raw_input in Python 3?
In Python 2: In Python 3, I get an error: NameError: name ‘raw_input’ is not defined Answer Starting with Python 3, raw_input() was renamed to input(). From What’s New In Python 3.0, Builtins section second item.
How can I partition (split up, divide) a list based on a condition?
I have some code like: The goal is to split up the contents of mylist into two other lists, based on whether or not they meet a condition. How can I do this more elegantly? Can I avoid doing two separate iterations over mylist? Can I improve performance by doing so? Answer How can I do this more elegantly? Th…
How do I reverse a string in Python?
There is no built in reverse function for Python’s str object. What is the best way of implementing this method? If supplying a very concise answer, please elaborate on its efficiency. For example, whether the str object is converted to a different object, etc. Answer Using slicing: Slice notation takes…
How do I get the last element of a list?
How do I get the last element of a list? Which way is preferred? Answer some_list[-1] is the shortest and most Pythonic. In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, al…
Relative paths in Python [duplicate]
This question already has answers here: How do you properly determine the current script directory? (16 answers) Closed 6 months ago. I’m building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don’t, however, have the absolut…
os.system() execute command under which linux shell?
I am using /bin/tcsh as my default shell. However, the tcsh style command os.system(‘setenv VAR val’) doesn’t work for me. But os.system(‘export VAR=val’) works. So my question is how can I know the os.system() run command under which shell? Answer os.system() just calls the syst…