Archive for the 'Python' Category
It’s really easy to delete a single file in Python. Say the file you want to delete is called “MyFile.txt.” You could delete it using this syntax:
import os
os.remove("MyFile.txt")
Let’s say you had many files in a directory, and the ones you wanted to delete were called MyFile1.txt, MyFile2.txt, and MyFile3.txt. One trick that you can use [...] Read more »
December 23rd, 2011 | Posted in Python | No Comments
If you have a list of String objects in Python and want to sort them but treat any numeric values accordingly, it will not work with the regular sort function in Python. So if you have the following list:
x = ["3", "1", "10", "2"]
You will get the following if you sort and print it:
print sorted(x) [...] Read more »
June 7th, 2011 | Posted in Python | 1 Comment
Let’s say you have a list of Python objects.
x = [["1","a"], ["2", "a"], ["3","b"], ["4","c"]]
And you have a second list of Python objects.
y = ["a", "b"]
Now let’s say you want to filter x so that it only contains objects for which the second value corresponds to the values in y. So in this case you [...] Read more »
March 9th, 2011 | Posted in Python | 4 Comments
In this post, I referred to an article that explains how to invoke methods dynamically (when you have the name of the method as a string object). Today I ran into a problem where I needed to do this, but the methods were not contained within a class. I just had them declared in my [...] Read more »
March 25th, 2010 | Posted in Python, Reflection | 2 Comments
Let’s say you did a search for files matching a certain pattern in a directory using Python:
import glob
filePaths = glob.glob("C:\\Temp\\*.txt")
print filePaths
This will list the full file paths with a .txt extension in the C:\Temp directory. For example: C:\\Temp\\test.txt.
But if you wanted to get just the file name, how would you go about that? It took [...] Read more »
December 3rd, 2009 | Posted in Python | 1 Comment
In Python 2.5, there is a built-in function that tells you the maximum allowable int value:
import sys
print sys.maxint
But there is no corresponding function to do this for float values. Part of the reason may be that it can vary from system to system. But actually, I believe they added this in version 2.6. So this [...] Read more »
July 8th, 2009 | Posted in Math, Python | No Comments
Suppose you have an object in Python that you retrieved from a third-party library, but you don’t have access to the source code or to very good documentation. Believe me, it happens (and did to me today). You can use a simple built-in method in Python to find out which methods are exposed by a [...] Read more »
June 17th, 2009 | Posted in Python, Reflection | 1 Comment
I recently had a scenario where part of what I wanted to do was in Java, and the other part was in an application that was written in Python. Rather than than rewrite my entire code base in one language or the other, I wanted to find a (quick and dirty) way to invoke the [...] Read more »
June 4th, 2009 | Posted in Java, Linux, Python, Tip | No Comments
In Java and C#, there is a method that enables you to find the last occurrence of a given sequence of string characters within a string. This method is sometimes called “lastIndexOf.” A method by this name does not exist in Python. But there is an easy way to do it if you know what [...] Read more »
April 16th, 2009 | Posted in Python | No Comments
Let’s say you want to create a sequence of numbers in reverse order (for example, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1). I tried to do this with the range function, but I couldn’t find a way to do it. But Python has a built-in function called reversed that allows you to [...] Read more »
January 14th, 2009 | Posted in Collections, Python | 2 Comments
To my knowledge, there is no built-in function in Python to find the mean of a list of numbers. You can use statistics packages to do this, such as statpy, but if you just want a lightweight solution to do the trick you can use the function below. Note that on the first line I [...] Read more »
January 2nd, 2009 | Posted in Math, Python, Statistics | 10 Comments
Sometimes you are working with an object in Python, and because Python is not a strongly typed language, you don’t always know which class it is an implementation of. A quick way to find out is to use the type method, which can be invoked on any object and gives you a String representation of [...] Read more »
December 31st, 2008 | Posted in Python, Tip | No Comments
In popular object-oriented languages such as Java and C#, they have a built-in method to each class that allows you to obtain a String representation of that class. They call these methods toString() or ToString(), respectively. You can use the default implementation, which basically gives you the name of the class (not usually very helpful). [...] Read more »
December 31st, 2008 | Posted in C#, Java, Python, Tip | 1 Comment
In Python, there is a bit of functionality that makes it convenient at times to process data while it is being processed inside another method. For example, let’s say you are searching for files within a directory that match a certain pattern, but you don’t want to wait the full time for the files to [...] Read more »
December 30th, 2008 | Posted in Python, Tip | No Comments
Let’s say you want to search for files in a directory tree of files and directories. And suppose you want to search for all .zip files. You can do this from within Python by looping through the folders and trying to find files that match the *.zip wildcard pattern.
Here is some code I copied from [...] Read more »
December 29th, 2008 | Posted in Python, Tip | No Comments
Sometimes you want to be able to invoke a method dynamically rather than calling it explicitly. Or in other words, let’s say you have a bunch of methods, and you have a string object telling you which method to execute, but rather than doing a bunch of if statements you want to be able to [...] Read more »
December 27th, 2008 | Posted in C#, Java, Python, Reflection, Tip | No Comments
One of the key properties of Python is that it is a weakly typed language. In contract to strongly typed languages such as Java, you don’t have to define the class of an object when you instantiate it. This offers much flexibility. But sometimes you want to be able to define precisely what class of [...] Read more »
December 27th, 2008 | Posted in Collections, Python, Tip | No Comments
Recently I had a bunch of one-page PDF documents that I wanted to be able to paste together so I can read them all as a single document. It turns out this is possible to do with Adobe Acrobat (which makes sense), but you have to pay for this product, and I wanted to do [...] Read more »
December 26th, 2008 | Posted in Python, Tip | No Comments
Let’s say you have a list of strings and want to be able to sort them. Python makes this extremely easy to do, which is something I really love about it.
x = ["abc", "def", "aaa"]
x.sort() // would result in [’aaa’, ‘abc’, ‘def’]
But what if you have numbers embedded in those strings? Most of us have [...] Read more »
December 26th, 2008 | Posted in Python, Tip | 1 Comment
Let’s say you are trying to retrieve the names of a bunch of files in a given directory that match a specific pattern. You can do this pretty easily at the command line using a command such as the following in Windows.
dir *.txt
Or something like this in Linux:
ls *.txt
But if you want to do this [...] Read more »
December 22nd, 2008 | Posted in Python, Tip | No Comments