当右键单击该文件夹时,从文件夹上执行的Python脚本生成的EXE文件

发布于 2025-01-18 09:47:05 字数 512 浏览 3 评论 0原文

我对Python有些新鲜,所以希望我的问题不是完全错误的。我有一个Python脚本,该脚本会在文件夹中扫描文件,并根据标准重命名其中一些。这样:

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

因此,每当我想在文件夹中的文件上执行此脚本时,我需要将脚本放在该文件夹旁边,将文件夹重命名为folder,然后运行脚本。我想知道,有什么方法吗?

1-将此脚本转换为独立的.exe文件,

2-将其添加到Windows的右键单击上下文菜单中,当我右键单击带有任何名称的任何文件夹时,我都可以请参阅.exe文件,

3-通过单击.exe文件,在该文件夹内的文件上执行重命名操作。

我要问的是什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2025-01-25 09:47:05

您始终可以使用像 pyinstaller 这样的库将其变成 .exe,但它不会是独立的,但是您可以使用 input() 将它们放入文件夹路径中,或者使用另一个包来启动文件资源管理器选择窗口,他们可以在其中选择一个文件夹,然后从那里开始。

希望这有帮助

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

£烟消云散 2025-01-25 09:47:05

可执行文件.exe是Windows的可执行文件。一般来说,术语“可执行文件”是指任何独立运行的程序。

脚本:Python 脚本不需要可执行。一般来说,Python 是可执行文件,而你的 python 脚本只是一个带有命令的文本文件。

目录:文件夹是 Windows 的东西,在编写脚本时被称为通用术语“目录”。

您描述的解决方案是可行的。这个问题可以通过多种方式解决,在 Windows 文件夹上使用右键菜单命令可能不是最好的方法。

创建“.exe”可能并不是您真正需要的。 Python 是“.exe”,Python 可执行文件将运行您的脚本 - 所以我们跳过它。

关于您描述的右键单击方法...如果这是一个您只想一次在一个文件夹上运行的脚本,那么它听起来很有用。如果您需要在很多文件夹上运行此脚本,最好在脚本中包含搜索并跳过右键单击菜单。或者您可以有一个文本文件,其中列出了目录路径。无论如何,如果您确实采用右键单击方法,这里有一些入门提示。

1) 在 Windows 操作系统中的文件夹中添加右键菜单项

如果上下文菜单太复杂,只需手动运行脚本并复制并复制即可。将文件夹路径粘贴为参数。如果您不知道如何做到这一点,那么您应该在进行这些集成之前继续学习基础知识。

2) 为脚本创建可执行文件

  • 上面的 .BAT 文件对此进行了介绍

3) 使用 python 重命名

我希望有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文