适用于 Windows 的带有 shebang 的 python 脚本
下面是 LINUX 上名为 nfile 的文件中的脚本:
#! /usr/bin/env python
import os
nfile = 0
for path,dlist,flist in os.walk('.'):
nfile += len(flist)
print("Number of files: {0}".format(nfile))
它位于我的 PATH 上的目录中,如果我在任何地方说“nfile”,它都会给我一个文件数量的计数(注意:我从不使用符号链接)。
我想在 Windows 11 上复制相同的功能。我了解到可以使用 Windows 的 Python Launcher 来获取该功能。
我使用 Anaconda Python,它不安装启动器。 Anaconda 用户如何安装启动器?
安装启动器后,我根本不清楚如何获得上述功能。我可以将“nfile”放在我的 Windows 路径中。我需要让它像在 Linux 上一样可执行吗?我是否在命令行中说“py nfile”或其他什么?
Below is a script in a file named nfile on LINUX:
#! /usr/bin/env python
import os
nfile = 0
for path,dlist,flist in os.walk('.'):
nfile += len(flist)
print("Number of files: {0}".format(nfile))
It is located in a directory that is on my PATH and if I say "nfile" from anywhere it gives me a count of the number of files (Note: I never use symbolic links).
I want to replicate the same functionality on Windows 11. I have been able to gather that the Python Launcher for Windows can be used to obtain that functionality.
I use Anaconda Python which does not install the launcher. How can Anaconda users install the launcher?
Once the launcher is installed, it is not at all clear to me how to get the functionality discussed above. I can put "nfile" on my Windows PATH. Do I need to make it executable as on Linux? Do I say "py nfile" at the command line or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否正确理解你的问题,但我猜你想从 Windows 命令行中的任何位置运行命令“nfile”。
这可以在另一个批处理 (.bat) 文件的帮助下实现。您需要在脚本旁边创建一个名为“nfile.bat”的文件。
脚本和批处理文件都需要位于 PATH 中定义的位置。
批处理文件的内容应包含以下内容:
Not sure if I understood your question correctly, but I guess you want to run the command 'nfile' from anywhere in Windows command line.
This is possible with the help of another batch (.bat) file. You need create a file called 'nfile.bat' next to the script.
Both the script and batch file need to be in a location that's defined in PATH.
The content of the batch file should contain something like: