python

Difference between binary and text I/O in python on Windows

I know that I should open a binary file using "rb" instead of "r" because Windows behaves differently for binary and non-binary files. But I don't understand what exactly happens if I open a file the wrong way and why this distinction is even necessary. Other operating systems seem to do fine by treating both kinds of files the same. ...

Executing Python program on web

Hi, Can I use os.system() or subprocess.call() to execute a python program on a webserver? I mean can I write these functions in a .py script and run it from a web browser and expect the program to be executed? Thanks a lot. EDIT: Sorry for all the confusion, I am giving you more background to my problem. I am trying to do is this. ...

is None vs. ==None

I recently came across this syntax, I am unaware of the difference. I would appreciate it if someone could tell me the difference. ...

Get IP Adress from a connected Windows Network Share with Python

How can i manage to get the IP or path like \11.1.1.100\projects of a connected network share with a drive letter. I only have the drive letter and want to get the IP of the Share with python. Many Thanks... Sashmo ...

pyinotify: Handling IN_MODIFY triggers

I am trying to watch a directory, and is looking for file modifications. Thinking of using pyinotify. Problem is that while using IN_MODIFY event to check for a file change, it triggers quite a number of events if I am copying even a small file of say 12 MB to the directory over a network. I dont want to handle so many triggers. I want ...

best way to implement custom pretty-printers

The documentation for the pprint module mentions that the method PrettyPrinter.format is intended to make it possible to customize formatting. I gather that it's possible to override this method in a subclass, but this doesn't seem to provide a way to have the base class methods apply line wrapping and indentation. Am I missing somethi...

Overcoming os.system() limitation in Python 2.3

I am having a problem converting one of my company's scripts from csh to Python. The csh script calls an aliased command, but when I call that same aliased command via os.system(), it does not work. So, if foo is the aliased command: CSH Script (this works, executes foo): foo <argument> Python (this does not work, error claims foo ...

for loop python

I have the fallowing code to encrypt a massage: massage= raw_input("Enter message to be encrypted: ") spec = chr(0b1010101) key = ord(spec) encrypt = "" for i in range(0, len(massage)): encrypt = encrypt + chr(ord(massage[i]) ^ key) print encrypt say I give "yo yo" to it it will give me : , ,: ,:u ,:u, ,:u,...

Converting from hex to binary without losing leading 0's python

I have a hex value in a string like h = '00112233aabbccddee' I know I can convert this to binary with: h = bin(int(h, 16))[2:] However, this loses the leading 0's. Is there anyway to do this conversion without losing the 0's? Or is the best way to do this just to count the number of leading 0's before the conversion then add it in...

Is there a folder layout structure for Django websites?

I am trying to get an overview of a Django website application structure. The way I have done this in the past with other frameworks (Symfony, RoR etc) is to look at the application folder structure, work out which bits go where, and then work my way on from there onwards. I have been searching online for similar info about Django websi...

Django + mod_python + apache: admin panel and urls don't work

Whole this day I was trying to configure django on production server. I use mod_python. When I open http: //beta.example.com I see my site but http: //beta.example.com/admin and http: //beta.example.com/441/abc/ doesn't work: Page not found (404) Request Method: GET Request URL: http://beta.example.com/admin {'path': u'admin'} ...

Pythonic way to insert every 2 elements in a string

Is there a pythonic way to insert an element into every 2nd element in a string? I have a string: 'aabbccdd' and I want the end result to be 'aa-bb-cc-dd'. I am not sure how I would go about doing that. ...

How do I do threading in python?

using code from this site: http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/ The code is import time from threading import Thread def myfunc(i): print "sleeping 5 sec from thread %d" % i time.sleep(5) print "finished sleeping from thread %d" % i for i in range(10): t = Thread(target=myfunc, args...

Is there a way to make a Python console application flash in the taskbar in Windows?

Is there a way to make my Python console application's window flash in the Windows taskbar, to get a user's attention? My script will be run exclusively in a relatively homogeneous Windows environment, so I don't care about detecting whether a particular API is present, or whether a solution is cross-platform or not (of course cross-pla...

Creating Python function with partial parameters.

I want to pass a Python function to another function with some of its parameters "filled out" ahead of time. This is simplification what I am doing: def add(x, y): return x + y def increment_factory(i): # create a function that increments by i return (lambda y: add(i, y)) inc2 = increment_factory(2) print inc2(3) # prints 5...

Writing bindings and wrappers

I keep seeing people writing wrappers for, say a module written in X language to use it in Y language. I wanted to know the basics of writing such wrappers. Where does one start from? My question here is more specific for libgnokii, how do I begin to write python bindings for it. ...

Perl's BEGIN{} block in Python

I have Python code that uses the "with" keyword (new in 2.6) and I want to check if the interpreter version is at least 2.6, so I use this code: import sys if sys.version < '2.6': raise Exception( "python 2.6 required" ) However, the 2.4 interpreter chokes on the with keyword (later in the script) because it doesn't recognize the ...

sorting a list of tuples

I have a list of tuples of the form (a,b,c,d) and I want to copy only those tuples with unique values of 'a' to a new list. I'm very new to python. Current idea that isn't working: for (x) in list: a,b,c,d=(x) if list.count(a)==1: newlist.append(x) ...

Help with Strange Python scraping error. HTTPError with one machine while it works on others.

I am using a proxy and following is the code. 20 req = urllib2.Request(url) 21 # run the request for each proxy 22 # now set the proxy 23 req.set_proxy(proxy, "http") 24 req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3') 25 req.add_hea...

If is in - Statement. Python.

What statemen should i use for this kind of task: I need finde if string variable a is in string variable b. What should I use? ...