Use custom python scripts from any directory in windows

In this one I am going to be showing you how you can run your python scripts from the terminal in directory on windows.

But first we shall look at what a PATH variable means and what ENVIRONMENTAL variables mean

Environmental Variable
An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.

PATH Variable
The PATH variable is an environment variable that contains an ordered list of paths that Linux will search for executables when running a command. Using these paths means that we do not have to specify an absolute path when running a command.

Now that we know what these terms mean we can get into the actual work
First step is to write our custom script, in this case I am using a script I developed to kill annoying tasks because task manager sometimes does not respond

<span>import</span> <span>os</span>
<span>import</span> <span>sys</span>
<span>if</span> <span>len</span><span>(</span><span>sys</span><span>.</span><span>argv</span><span>)</span> <span><</span> <span>2</span><span>:</span>
<span>print</span><span>(</span><span>"</span><span>[!] Usage: kill.py </span><span>'</span><span>program name</span><span>'"</span><span>)</span>
<span>sys</span><span>.</span><span>exit</span><span>()</span>
<span>program</span> <span>=</span> <span>sys</span><span>.</span><span>argv</span><span>[</span><span>1</span><span>]</span>
<span>print</span><span>(</span><span>f</span><span>"</span><span>[*] Killing </span><span>{</span><span>program</span><span>}</span><span>"</span><span>)</span>
<span>os</span><span>.</span><span>system</span><span>(</span><span>f</span><span>'</span><span>taskkill -f -im </span><span>{</span><span>program</span><span>}</span><span>.exe</span><span>'</span><span>)</span>
<span>sys</span><span>.</span><span>exit</span><span>()</span>
<span>import</span> <span>os</span>
<span>import</span> <span>sys</span>

<span>if</span> <span>len</span><span>(</span><span>sys</span><span>.</span><span>argv</span><span>)</span> <span><</span> <span>2</span><span>:</span>
    <span>print</span><span>(</span><span>"</span><span>[!] Usage: kill.py </span><span>'</span><span>program name</span><span>'"</span><span>)</span>
    <span>sys</span><span>.</span><span>exit</span><span>()</span>

<span>program</span> <span>=</span> <span>sys</span><span>.</span><span>argv</span><span>[</span><span>1</span><span>]</span>
<span>print</span><span>(</span><span>f</span><span>"</span><span>[*] Killing </span><span>{</span><span>program</span><span>}</span><span>"</span><span>)</span>
<span>os</span><span>.</span><span>system</span><span>(</span><span>f</span><span>'</span><span>taskkill -f -im </span><span>{</span><span>program</span><span>}</span><span>.exe</span><span>'</span><span>)</span>
<span>sys</span><span>.</span><span>exit</span><span>()</span>
import os import sys if len(sys.argv) < 2: print("[!] Usage: kill.py 'program name'") sys.exit() program = sys.argv[1] print(f"[*] Killing {program}") os.system(f'taskkill -f -im {program}.exe') sys.exit()

Enter fullscreen mode Exit fullscreen mode

Next step is to create a folder for our custom scripts and add the folder path to environmental variables
图片[1]-Use custom python scripts from any directory in windows - 拾光赋-拾光赋
After copying folder path click windows button and
图片[2]-Use custom python scripts from any directory in windows - 拾光赋-拾光赋
Next step click on environmental variables
图片[3]-Use custom python scripts from any directory in windows - 拾光赋-拾光赋
click on path and then edit at the bottom
图片[4]-Use custom python scripts from any directory in windows - 拾光赋-拾光赋
click on new and then paste in the path to our custom folder
图片[5]-Use custom python scripts from any directory in windows - 拾光赋-拾光赋
And voila we are ready to go. So to test this we open a new terminal and type out the name of our script in this case kill.py you should see the error output
Anyways we go ahead and test this

Use this to automate your boring tasks, Happy Coding Everyone 🙂

原文链接:Use custom python scripts from any directory in windows

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
The worst sort of indolence is being given a choice, yet taking no initiative to change.
我们人生中最大的懒惰,就是当我们明知自己拥有作出选择的能力,却不去主动改变而是放任它的生活态度
评论 抢沙发

请登录后发表评论

    暂无评论内容