While working on some code for an 16×2 LCD display on my Raspberry Pi I wanted a way to rotate a text string in a specified direction and by a specified number of characters. The idea was to use it to scroll text across the screen.
The following post shows you various examples of listing files in Python. You can easily include or exclude files based on size and age.
The example use the os.walk function which grabs filename from a directory structure. The file age and size are extracted using the os.stat function.
Following on from my How To Combine Text And CSV Files With Python article I decided to create a script to combine CSV or Text files into one file. This isn’t something I need to do on a regular basis but when I do automating it saves a lot of time.
The script is fairly basic. It opens each file within a specified directory, reads the content and appends it to a specified output file.
Following on from my Sending Text Email In Python article here is a slightly more advanced script that sends HTML formatted emails. The mechanism is fairly similar.
As with the text example you need access to an SMTP server which you will probably have if you have a web hosting account or ISP that allows you to send email.
Despite the march of technology plain text files are still a popular way to transfer and process data. Comma Separated Value (CSV) files are still used to import and export data between spreadsheets, databases, online tools and other data repositories.
The script shown below will take a text file and split it into a number of smaller files based on a specified line count. This works for normal text as well as CSV files. I use it to split large data sets into smaller batches for import into database systems.
Lists are a useful concept in Python and in many ways are similar to arrays in other programming languages. Python provides a number of list methods which allow you to manipulate the elements within and these are documented in the official data structures documentation.
One thing I needed to do recently was to take a list of data and split it into a number of smaller chunks. I wanted to specify the number of the chunks and the elements of the list to be split appropriately,
In the example below the function “get_chunks” is defined. It accepts two arguments. The first is a list of data and the second is the number of chunks you require.
Python includes a standard library to generate random numbers. This library is called “random”. In order to start generating random numbers in Python you must import the random library at the start of your script.
When you import the library the generator is seeded using the current system time so you may see different results every time you run your script.
One task I use Python to perform on my desktop is image manipulation. Although there are plenty of good image processor utilities (i.e. IrfanView and XnView) sometimes it is simply quicker to create a Python script to complete a particular task for you.
By importing the Image library and making use of the resize function images can be resized to a specified size. It supports all common image formats such as PNG, GIF, JPG and BMP.
Sending email in Python is easy if you have access to an SMTP server. These are often provided as part of a web hosting account or by email providers. The details are those that would be required to send email using Outlook, Thunderbird or other third-party mail client.
The required SMTP details are:
- SMTP server domain