TURFPTAx Python Code Make python scripts the default application for specific extensions on Windows

Make python scripts the default application for specific extensions on Windows

Make python scripts the default application for specific extensions on Windows post thumbnail image

Subtitle: Using FTYPE to run a python script as its default applications for any extension on windows .whatever

Do you have a python script that you want to run on a file of a certain extension like a .dat file? or a .custom?

Using FTYPE command prompt you can accomplish this easily.

In this scenario I had a friend need a document meant for another program to display legibly instead of showing up all turpfy taxy.

Step 0: Creating the python script to Handel arguments using sys.argv

add this line to your python script:

import os, sys

#sys.argv[0] will be the location of the python script
# will explain this later on
# Open the file that is double clicked on
customFile = open(sys.argv[1])

# read the file
customFile = customFile.Read()
# Keep the CMD app from closing


Step 1: Get windows to run your python script as the default application for the extension

Now that we have the python script we will need to open CMD as an administrator to make it open up the script and pass the file location to our script.

Open cmd.exe as an administrator:
Press the windows key, type cmd, hold ctrl + shift and hit enter.

Now you will want to type the following two lines:

FTYPE CustomNameHere=<location of python.exe> <location of python script.py> “%1” %*

ASSOC .customextention=CustomNameHere

FTYPE AAA=C:\Python.exe C:\coolScript.py “%1” %*


Once you typed those two lines into CMD you will notice that explorer.exe will refresh. now if you navigate to a file with the .WTFudge extension it will have a little python executable symbol now.

You will notice that we are %1 to your python script and that is being ran by python.exe so argv[0] will be the location of your python script and argv[1] will be %1 which is the location of the file you double clicked on. So you could pass other variables if you wish into this.

Step -1: If you double click on a file and it just displays the code of your script and not its output…

If you double click on an extension and cmd outputs the code of the script instead of its output you will have to check your syntax. Python errors will cause the instance to display the code instead of executing if it runs into an error. So an easy way to test this is to comment out all code and just do a print(‘hello world’). To make sure it is working.

Want to run a python program from the right click menu on windows?

Use a similar trick by going to the following website: https://stackoverflow.com/questions/6852833/running-a-batch-script-by-right-clicking-any-file

By adapting the script you can run python scripts on any program using the right click menu!!!

Batch Script from the above website:
@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %~dpnx0
echo Subject is %1 %~dpnx1

Hope this helps all!

Related Post