Alias in Windows and it’s Fun Applications using Python

n0obcoder
Analytics Vidhya
Published in
6 min readJun 6, 2020
Alias for Justice

I was wondering how could I possibly get a python script to run automatically every time I open the command-line. I believe that it could have some fun applications.

I imagined opening up the command-line and writing

todo “Learn Ableton Live 10”

to add “Learn Ableton Live 10” to a To-Do List (.txt file).

But how could I pull this off? I mean, obviously, the command-line does not recognize ‘todo’ command and how would I call a python script by hitting ‘todo’ anyway…

This is where ‘Alias’ comes into the picture!

What is an Alias?

An alias, as the name refers provides an alternate name for existing names. Alias is usually an easier-to-understand name for a defined data object.

For you to understand the idea of alias, I am going to say that “sup” is an alias to “wassup” which itself is an alias to “what’s up” 😛

So we are basically talking about making our own lingos in programming. Doesn’t it sound cool already? 😎

How is Aliasing Useful?

If you type ‘cls’ in a Windows command-line or ‘clear’ in Linux terminal and hit enter, it clears everything out from there. We could make an alias ‘c’ just to perform the same task.

Also, we often have a directory which we frequently have to visit via the terminal by using the ‘cd’ command. It becomes very frustrating to use the ‘cd’ command every time we open a new terminal to enter into that directory, especially when the directory has a very long path. This is another appropriate use case of setting up an alias.

And similarly, we can make aliases for various different commands that are valid for a command-line.

How to Create an Alias in Windows?

Let’s now learn how to create an alias. Belonging to an intelligent species, you might have already figured out how to create an alias, after seeing the 2 examples that I used earlier.

You got that correct!

We use ‘doskey’ command to create an alias. We write ‘doskey’, then the alias name followed by the equal to sign (‘=’ ) further followed by the command that the alias is supposed to run.

How to Call an Alias?

Once we are done creating the alias, calling them is as simple as typing the name of the alias and hitting the enter key in the command-line!

Let’s see the alias ‘teleport’ that we created earlier in action now.

Creating an Alias for a Python Program?

Now that we know how to create an alias, it seems pretty straightforward to figure out the way how we can create an alias for calling a python script.

We use the ‘doskey’ command and assign the command for running a specific python script, to an alias using the equal to sign (‘=’).

Here my alias named “todo” is supposed to run the python script called “todo_list_maker.py” which is located at “C:/Users/anime/Documents/Python Scripts/todo_list_maker/”.

The python script “todo_list_maker.py” takes an input (a to-do list item) from the command-line and puts it in a todo_list.txt

But how do we pass in the arguments to our python script?

It is actually quite simple. All we need to do is add a dollar sign and an asterisk (“$*”) at the end of the alias assigning step in the way it is mentioned down below. It allows the inputs to the python script to be passed while calling the alias.

I have used argparser for parsing the arguments from the command-line in todo_list_maker.py

Mentioned below is the python script ‘todo_list_maker.py’

And following is the command that we need to use in the command-line to call the alias “todo” and add a to-do list item in the to-do list.

And as expected it generates a to-do list (.txt file) on the Desktop (by default)

and adds the to-do list item in it.

So this is how we create an alias for calling a python script and pass arguments in it through the alias.

But there is a last piece of information that I would like to add in this blog. The aliases do not work when we try to call them in a new command-line. This can be fixed by making a Batch or BAT file (.bat file) which would hold the command(s) that would run every time a new command-line terminal is opened.

In Windows, the batch file is a file that stores commands in a serial order. Command-line interpreter takes the file as an input and executes the stored commands in the same order. A batch file is simply a text file saved with the .bat file extension. It can be written using Notepad or any other text editor.

So we make a BAT file and save it at any location on the computer (Note this location as you would be using this location in a short while). Following is the BAT file that I made. It will first execute the “echo” command and then then the “doskey” command, each and every time a new command-line is opened.

Now we go to the Windows Search option and type “cmd” and then click on “Open file location” option.

We then right-click on the “Command Prompt” icon and select the “Properties” option.

Then we select the “Shortcut” tab and add

“/k C:\Users\anime\doskey_for_python.bat” (/k path_of _the _bat_file) in the “Target” field.

Hit “Apply” and ‘OK’ and we are done.

Now every time we open a new command-line, the BAT file is executed and our alias is ready to be used. This holds true even after we restart the computer which is exactly how we would want it to be.

So finally our “todo” command is ready to be used in the command-line to add new to-do list items in the to-do list.

We can now combine the idea of alias with the infinite functionalities that python brings to the table, to make fun commands do useful tasks!

Code

You can find the code for this “todo list maker” application on my GitHub account by clicking on this link https://github.com/n0obcoder/fun-with-python/tree/master/todo_list_maker

The README.md file provides a decent documentation of the todo_list_maker.py. Please feel free to explore it and add more functionalities to it 🙂

I am writing this blog because I have learned a lot by reading other’s blogs and I feel that I should also write and share my learnings and knowledge, as much as I can. So please leave your feedback in the comments section below to let me know how can I improve my future blogs! :D

Also I am an independent music artist who likes to play and record music in free-time. Maybe you can check out my artist page and show your support : )
8th Floor Harmony on Spotify!

--

--

n0obcoder
Analytics Vidhya

DL Engineering in the making and a Struggling Musician