0

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?

tajir12457
  • 153
  • 1
  • 11
  • If I had to answer I'd say it isn't possible, but I'm interested to see where this goes. Typically python scripts are not compiled into an exe file, but there are ways to do so. The most common way of running python code is as a script executed by the python interpreter. The part about showing up in a right click menu is not something you can do with python, that would be some sort of windows thing. Generally I'd say there has got to be a better way to approach the problem. – Brad Fallon Apr 01 '22 at 23:51

2 Answers2

2

You could always use a library like pyinstaller to make it into a .exe, but it wouldn't be standalone, however you could use input() to have them put in the folder path, or use another package to initiate the file explorer choose window, in which they could select a folder and then go from there.

Hope this helps

S6ad0w
  • 63
  • 5
  • Pyinstaller is a really good way of turning python scripts into standalone executables. However, the question suggests that there is still some learning to do on how script parameters work, and how scripts can use operating system facilities like being called from a context click. Reading up on [argparse](https://docs.python.org/3/library/argparse.html) should be a good starting point. – Powertieke Apr 02 '22 at 06:55
1

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!

Brad Fallon
  • 169
  • 1
  • 7
  • Your answer helped me greatly, and yes, I do want to run the script on one folder at a time. There is only one thing left: what if I want to run the python script on a folder in a computer on which no python is installed? That's basically why I asked about turning the script into a standalone executable :) – tajir12457 Apr 02 '22 at 22:27