Search code examples
pythonexecontextmenuright-click

Exe file generated from python script executed on a folder when the folder is right-clicked on


I'm somewhat new with with Python, so hopefully my question is not entirely wrong. I have a python script, which scans the files inside a folder, and renames some of them based on a criteria. Like this:

files = [f for f in os.listdir("./Folder")]
...
...
...
os.rename("./Folder/" + old_name, "./Folder/" + new_name)

So, whenever I want to execute this script on files inside a folder, I need to place the script next to that folder, rename the folder to Folder, and run the script. I was wondering, is there a way I can;

1- convert this script to a standalone .exe file,

2- add it to the right-click context menu of Windows, in a way that when I right click on any folder with any name, I can see the .exe file,

3- perform the renaming operation on the files inside that folder by clicking on the .exe file.

Is what I'm asking even doable?


Solution

  • Executable: An .exe is an executable file for windows. In general, the term executable is any program that runs standalone.

    Script: A python script does not need to be executable. Generally, Python is the executable file and your python script is just a text file with commands.

    Directory: Folders are a windows thing and are referred to as the generic term "directory" when scripting.

    The solution you described is doable. This problem can be solved in a lot of ways, and having a right-click menu command on windows folders might not be the best approach.

    Creating an ".exe" is probably not really what you need. Python is the ".exe" and the Python executable will run your script - so let's skip that.

    Regarding the right-click approach you described... it sounds useful if this is a script you will only want to run on one folder at a time. If you are going to be needing to run this script on a lot of folders, it is probably better to just include a search in the script and skip the right-click menu thing. Or you could have a text file where you list the directory paths. Anyway, here are some tips to get started if you do take the right-click approach.

    1) Adding a right-click menu item to folders in Windows OS

    If the context menu thing is too complicated just run the script manually and copy & paste the folder path as an argument. If you don't know how to do that you should keep learning the basics before taking on these integrations.

    2) Creating an executable for your script

    • This is covered above with the .BAT file

    3) Use python to rename

    I hope that helps!