How do I limit os.walk to only return files in the directory I provide it? Answer Use the walklevel function. It works just like os.walk, but you can pass it a level parameter that indicates how deep the recursion will go.
Tag: python
How to get method parameter names?
Given that a function a_method has been defined like Starting from a_method itself, how can I get the argument names – for example, as a tuple of strings, like (“arg1”, “arg2”)? Answer Take a look at the inspect module – this will do the inspection of the various code object properties for you. The other results are the name of
Binary search (bisection) in Python
Is there a library function that performs binary search on a list/tuple and return the position of the item if found and ‘False’ (-1, None, etc.) if not? I found the functions bisect_left/right in the bisect module, but they still return a position even if the item is not in the list. That’s perfectly fine for their intended usage, but
How can I make a dictionary (dict) from separate lists of keys and values?
I want to combine these: Into a single dictionary: Answer Like this: Voila :-) The pairwise dict constructor and zip function are awesomely useful.
List of IP addresses/hostnames from local network in Python
How can I get a list of the IP addresses or host names from a local network easily in Python? It would be best if it was multi-platform, but it needs to work on Mac OS X first, then others follow. Edit: By local I mean all active addresses within a local network, such as 192.168.xxx.xxx. So, if the IP
What is the difference between re.search and re.match?
What is the difference between the search() and match() functions in the Python re module? I’ve read the Python 2 documentation (Python 3 documentation), but I never seem to remember it. I keep having to look it up and re-learn it. I’m hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head.
Python: can I have a list with named indices?
In PHP I can name my array indices so that I may have something like: Is this possible in Python? Answer This sounds like the PHP array using named indices is very similar to a python dict: See http://docs.python.org/tutorial/datastructures.html#dictionaries for more on this.
Finding a public facing IP address in Python?
How can I find the public facing IP for my net work in Python? Answer This will fetch your remote IP address If you don’t want to rely on someone else, then just upload something like this PHP script: and change the URL in the Python or if you prefer ASP: Note: I don’t know ASP, but I figured it
Python Dependency Injection Framework
Is there a framework equivalent to Guice (http://code.google.com/p/google-guice) for Python? Answer I haven’t used it, but the Spring Python framework is based on Spring and implements Inversion of Control. There also appears to be a Guice in Python project: snake-guice
Character reading from file in Python
In a text file, there is a string “I don’t like this”. However, when I read it into a string, it becomes “I donxe2x80x98t like this”. I understand that u2018 is the unicode representation of “‘”. I use command to do the reading. Now, is it possible to read the string in such a way that when it is read