Top "Python-2.x" questions

For questions about Python programming that are specific to version 2.x of the language.

General Unicode/UTF-8 support for csv files in Python 2.6

The csv module in Python doesn't work properly when there's UTF-8/Unicode involved. I have found, in the Python documentation …

python csv unicode utf-8 python-2.x
python 2 code: if python 3 then sys.exit()

I have a large piece of Python 2 only code. It want to check for Python 3 at the beginning, and exit …

python python-3.x python-2.x
Python, default keyword arguments after variable length positional arguments

I thought I could use named parameters after variable-length positional parameters in a function call in Python 2, but I get …

python variadic-functions python-2.x named-parameters default-parameters
Mock Python's built in print function

I've tried from mock import Mock import __builtin__ __builtin__.print = Mock() But that raises a syntax error. I've also tried …

python unit-testing mocking python-2.x
Python: How to resize an image using PIL module

I'm trying to resize an image to 500x500px but got this error: File "C:\Python27\lib\site-packages\PIL\Image.…

python python-2.7 python-2.x image-resizing
StringIO and compatibility with 'with' statement (context manager)

I have some legacy code with a legacy function that takes a filename as an argument and processes the file …

python python-2.x with-statement contextmanager
In Python 2, how do I write to variable in the parent scope?

I have the following code inside a function: stored_blocks = {} def replace_blocks(m): block = m.group(0) block_hash = sha1(…

python closures scope python-2.x
Use of input/raw_input in python 2 and 3

I would like to set a user prompt with the following question: save_flag is not set to 1; data will …

python python-3.x input python-2.x raw-input
overriding bool() for custom class

All I want is for bool(myInstance) to return False (and for myInstance to evaluate to False when in a …

python class casting boolean python-2.x
Swapping uppercase and lowercase in a string

I would like to change the chars of a string from lowercase to uppercase. My code is below, the output …

python string python-2.x uppercase lowercase