How to join PDF files in Mac on Terminal?

In Mac, you can use Preview to join multiple pdf files into a single pdf file. A more convenient way is to join/merge multiple pdf files from command line on Terminal. More recent Mac OS X versions already have Python script to merge multiple pdf files into a single pdf file. Here is how to […]

How to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?

You might want to convert a really long string in a single line into multiple lines with specified width. For example, if you have string like 12345678912345678912345678912345678912345678910 and want to split the long text to multi-line text, where each line is of length w, like w=9 123456789 123456789 123456789 123456789 123456789 10 In the above […]

“with” statement in Python to Open a file

A common way to work with files in Python is to create file handler with “open” statement and work with the file. After finishing the work with the file, we need to close the file handler with close statement. For example, if we want to read all lines of a file using Python , we use […]

How to Run a Shell Command from Python and Get The Output?

In Python, often you may want to execute linux command and get the output of the command as string variable. Naive way to do that is to execeute the linux command, save the output in file and parse the file. A better way to get the output from executing a linux command in Python is […]

Eleven IPython Keyboard Shortcuts to Move Around Easily

Eleven IPython keyboard tips/shortcuts to Move Around Easily. Ctrl-p or up-arrow: Access command history in backward Ctrl-n or down-arrow: Access command history  in forward Ctrl-a: Move cursor to beginning of line Ctrl-e: Move cursor to end of line Ctrl-f: Move cursor forward one character Ctrl-b: Move cursor back one character Ctrl-l: Clear screen Ctrl-r: reverse history […]

How To Paste Code in Python Interpreter? Hint: Use iPython

If you have formatted code block with proper indentation and want to test it by copying and pasting on a python interpretor, you will be frustrated at Python for complaining at you. iPython offers great solutions for copying and pasting the code right from the terminal interpreter. One useful command in iPython to play with […]

Three Ways to Write Text to a File in Python

If you are interested in writing text to a file in Python, there is probably many ways to do it. Here is three ways to write text to a output file in Python.  The first step in writing to a file is create the file object by using the built-in Python command “open”. To create and […]

7 Tips to Play With File Names, Directory Names, and Paths Using Python OS.path Module

Python’s OS.path module have a lots of commands/methods that are greatly useful for all things files/directories/paths. Here is seven tips for playing with files, directories, and paths using using Python os.path. How To Check If a “Name” is a Directory? To make sure if a name is directory or not, one can use “os.path.dir” to […]

Getting Started with Sort in Python: Sorting Lists in-place

Let us starting simple and start with easy sort. Python has two sort functions; one function that sorts a list in-place and the other sort function “sorted” that creates a new sorted list. In this post, we will see examples of “in-place” sorting. Python’s in-place sorting As the name suggests the in-place sorting replaces the […]

Mastering Lists in Python Using List Comprehensions

Ever faced the problem of creating a unique lists in Python? or Accessing only specific elements of lists in Python? Python’s “List Comprehensions” offers you the immense power to create and use lists. Here is a brief introduction to understand and getting started on using List Comprehensions in Python. Python’s List Comprehensions Example 1 The […]