python

Opening and Printing files using Recursion in python

So I am trying to write a code that opens a file and inside that file might be empty or contains the name of other files to open on each line. For example. 1.txt has 2.txt. on the first line and 3.txt on the second line. 2.txt is a empty file and 3.txt has 4.txt on the first line. I have to have an output that prints the files that the c...

Learn Python 2.x from Dive Into Python 3?

I keep hearing that Dive Into Python 3 is more pythonic than Dive Into Python. I have some experience in programming, but at the same time I'm new to Python (only went thru the official tutorial so far). I will be using Python 2.x at work. Would it make sense to use Dive Into Python 3 to learn Python 2.x? ...

"Pre-importing" a variable into a module.

Python beginner here, so I apologize if this question has a simple answer. (I hope it does.) I am working on a python module--a plugin for a larger program. I'm trying to develop the module using the Eclipse IDE (with pydev), which means I need to be able to run this module stand-alone, i.e. not as a plugin from the larger program. ...

Cannot change global variables in a function through an exec() statement?

Why can I not change global variables from inside a function, using exec()? It works fine when the assignment statement is outside of exec(). Here is an example of my problem: >>> myvar = 'test' >>> def myfunc(): ... global myvar ... exec('myvar = "changed!"') ... print(myvar) ... >>> myfunc() test >>> print(myvar) test ...

Is there any difference between UserDict and Dict?

If I want a class to have a dictionary behavior, why should I inherit from dict or UserDict? ...

Python C-API module exit handler - an atexit equivalent?

I'm using Python ver 2.6.4 There is a function I have to call from a C library when my extension module exits/is unloaded. What would be the equivalent of atexit for a C extension module? ...

what is the encoding of this string,'base64' or 'utf-8'??? ,how can i get it readable.

print "4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf".decode('base64')#no thanks and if i have '4-12个英文字母、数字和下划线' how can i get the string '4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf' is print '4-12个英文字母、数字和下划线'.decode('what')# i write: print u'4-12个英文字母、数字和下划线...

How to retry after exception in python?

Hi all, I have a loop starting with for i in range(0, 100), and inside the loop, normally it would run correctly, but sometimes due to network conditions it would fail. Currently i have it set so that when failed, it would continue in except clause (continue on to the next number for i). I was wondering whether it is possible for me to...

Checking for value within same type in python

a = 0 if a == False: print a in php I can say: $a = 0; if $a === false { echo $a; } The triple === in php check for the value within the same type, thus making the integer 0 not be read as a boolean value False How can I do this in python? I would like to differentiate between 0 the integer and False the boolean value in a si...

How would I discover the memory used by an application through a python script?

Recently I've found myself testing an aplication in Froglogic's Squish, using Python to create test scripts. Just the other day, the question of how much memory the program is using has come up, and I've found myself unable to answer it. It seems reasonable to assume that there's a way to query the os (windows 7) API for the information,...

create a .csv file with values from a Python list

I am trying to create a .csv file with the values from a Python list when I print the values in the list they are all unicode (?) i.e. they look something like this [u'value 1', u'value 2', ...] if I iterate through the values in the list i.e. for v in mylist: print v they appear to be plain text and I can put a ',' between each with ...

Windows process management using Python

I need a script that check if a particular process is running and return something if not found. I know that this can be done using subprocess, but is there a simpler way to do it? ...

Where (at which point in the code) does pyAMF client accept SSL certificate?

Hello, I've set up a server listening on an SSL port. I am able to connect to it and with proper credentials I am able to access the services (echo service in the example below) The code below works fine, but I don't understand at which point the client accepts the certificate Server: import os.path import logging import cherrypy fro...

md5 module error

I'm using an older version of PLY that uses the md5 module (among others): import re, types, sys, cStringIO, md5, os.path ... although the script runs but not without this error: DeprecationWarning: the md5 module is deprecated; use hashlib instead How do I fix it so the error goes away? Thanks ...

clear terminal in python

Does any standard "comes with batteries" method exist to clear the terminal screen from a python script, or do I have to go curses (the libraries, not the words) ? ...

sendinput to directinput(like games)

I'm trying to simulate keypress to my games that use direct input(I guess). I googled around and I found out the method SendIput(). It work fine if I try to send keypress to notepad.exe but nothing happend when I tried to games. I checked this site http://www.elitepvpers.de/forum/war-hacks-bots-cheats-exploits/170258-simulating-keystrok...

Extract links from a webpage using lxml, xpath and python

I've got this xpath query: /html/body//tbody/tr[*]/td[*]/a[@title]/@href It extracts all the links with the title attribute - and gives the href in FireFox's Xpath checker add-on. However, I cannot seem to use it with lxml. from lxml import etree parsedPage = etree.HTML(page) # Create parse tree from valid page. hyperlinks = parsedP...

What is the difference between these codes, and what does the repr do ?

1. >>> s = u"4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf" >>> print s 4-12个英文字母、数字和下划线 >>> print repr(s) u'4-12\u4e2a\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf' 2. print repr("4-12个英文字母、数字和下划线") '4-12\xb8\xf6\xd3\xa2\xce\xc4\xd7\xd6\xc4\xb8\xa1\xa2\xca\xfd\xd7\xd6\xba\xcd\xcf\x...

Running syncdb on Django project not working

When I run: $ python manage.py syncdb I get the following output: Creating table auth_permission Traceback (most recent call last): File "manage.py", line 11, in <module> execute_manager(settings) File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager utility.execute() ...

timeout for urllib2.urlopen() in pre Python 2.6 versions

The urllib2 documentation says that timeout parameter was added in Python 2.6. Unfortunately my code base has been running on Python 2.5 and 2.4 platforms. Is there any alternate way to simulate the timeout? All I want to do is allow the code to talk the remote server for a fixed amount of time. Perhaps any alternative built-in library...