Archive for the 'Python' Category

How to Encrypt and Decrypt Files in Python

If you want to be able to encrypt and decrypt files with Python, you might check out the following sites:
http://www.dlitz.net/software/pycrypto/
http://www.amk.ca/python/code/crypto.html
However, if you know you’re going to only be using Linux, I recommend doing file encryption as explained in this post. You can invoke commands at the command line by following the explanation here. Read more »

How to Modify PYTHONPATH on Linux

When you are installing modules in Python that need to be available to Python scripts stored in various locations on the server, you can use PYTHONPATH to specify where those resources are on the server. This environment variable tells Python where to find those modules/libraries.
This post explains how to set environment variables in Linux. You [...] Read more »

How to Generate a Sequence of Numbers in Python

Let’s say you want to generate a sequence of numbers from 1 to 10 in Python. Here’s how:

x = range(1, 11) // it goes up to one less than the second parameter

Let’s say you want to generate a sequence from 0 to 10:

x = range(11) // default is to start at 0 Read more »

How to Run Python Scripts from the Command Line in Windows

After installing Python and creating a script, all you have to do is open the command prompt and type the name of the script file (and pass any parameters to the script after that).
For example, in Windows XP, you would go to the Start menu, click on Run…, and enter cmd. Then you would need [...] Read more »

Find the Difference Between Two Lists with Python

Let’s say you have two lists in Python that have a lot of overlap between them.

a = ["abc","def","ghi"]
b = ["def","ghi","jkl"]

Now let’s say you want to determine what is in b that is not in a. In set theory, you would refer to this as the set-theoretic difference of b in a. The following simple code [...] Read more »

How to Publish to a WordPress Blog Using XmlRpc

This is the first in a series of posts about how to publish to a WordPress blog using an interface called XmlRpc that is supported by WordPress as well as other blogging platforms.
My motivation for writing these posts is that I have a hobby project that is written in the C# language, and I wanted [...] Read more »

Determine Whether a String Contains a Substring in Python

x = "abcdefg"
 
print "abc" in x // True
print "xyz" in x //False Read more »

How to Delete Files in a Folder with Python

Python has a function to help you delete directories and the files in the them, but it’s important to be very careful with this, because it will delete everything recursively without a warning.

import shutil
shutil.rmtree("C:\\Temp\\DeleteMe") Read more »

Invoke a Command-line Program Using Python

In Windows, Linux, MacOS, etc. you can either start up programs using the user interface (pointing and clicking with your mouse) or from a command line. When you run something from the command line, it does basically the same thing behind the scenes, but you type commands instead.
For example, in Windows, you can open Internet [...] Read more »

Convert a Delimited String to a List in Python

Let’s say you have a string with values that are delimited by a comma. This would be common when parsing text files.
So suppose you have a delimited string:

x = "abc,def,ghi"

You can convert that into a Python list using the split function, passing the character that is delimiting the items:

y = x.split(",")
print y // [’abc’, ‘def’, [...] Read more »

Log Transformations in Python

It is very simple to do a log transformation in Python. Log transformations are sometimes performed on a set of numbers to smooth out the data (make it look more “Normal”), which can help in performing certain statistical analyses.
The default in Python is the natural log. So for demonstration purposes, I will first find e^10. [...] Read more »

Copy Files Recursively in Python

import shutil
shutil.copytree(src, dst)

See also Copying Files in Python. Read more »

Copying Files in Python

To copy a file in Python, you would use the following syntax where src is the path to the file you are copying and dst is the path to which you want to copy the file.
import shutil
shutil.copy(src, dst)
If you want to copy files in a directory structure, you can use similar syntax where src is [...] Read more »

Convert Python List to a Delimited String

This is easy to do with the join command, though the syntax may not be immediately obvious.
So if you create a list:

x = ["abc","def","ghi","jkl"]

And wanted to convert that to a comma-separated list (for example, to store in a file), you would do this:

“,”.join(x)

And it would result in:

‘abc,def,ghi,jkl’

Or if you wanted it to be a tab-delimited [...] Read more »

Determine Whether a String Value is Numeric in Python

It is pretty easy to determine whether a String object is an integer in Python, but the functionality is a bit obscured by a misnomer. This can be done using a function called isdigit.
x = “123″
x.isdigit() // True
However, this has some limitations. It only works with deciphering positive integers. For example:
x = “1.23″
x.isdigit() // False
y [...] Read more »