0

Now I am running the python file everytime like this:

python C:/my/path/to/python/file.py

I want to replace it with one word command like this

myfile

The same command should work from command_prompt, powershell and powershell_ise and it should work from any directory that I am in.

1
  • Why don't you use a text editor like VSCode? You could even create your custom keyboard shortcut over there to run any program file. Like mine is F5, so just clicking F5 runs the program on the terminal Commented Jul 26, 2021 at 5:37

2 Answers 2

1

If you made the python an exe and cd into the directory or add it to your environment variables you could call it by myfile or myfile.exe

Another way would be to make a batch file that contains

python C:/my/path/to/python/file.py

and name it myfile which would allow you to call the batch file and it would type out the rest

Also if you cd into C:/my/path/to/python you could type python file.py instead of the whole path.

Sign up to request clarification or add additional context in comments.

Comments

1

You can basically create Custom Environment Variables. This link will guide how can you do that.

Link: Create Custom Environment Variables in Windows

The file option is interesting because it means you can also create an environment variable to launch a program. For example, you can point an environment variable to any EXE file on your system. When you invoke the variable, it will launch the program. If you have a custom executable program file stored in some random directory on your PC, this is an easy way to launch it without having to go look for it.

Or if you want to run your python script from anywhere, technically not anywhere but from other directory also then you can create a .bat file. And opening that .bat will automatically run your python script. You can create a .bat by :

  1. Right click create a new file. Name it as script.bat.
  2. Right click on script.bat and open it in Notepad.
  3. Copy/paste this script python C:/my/path/to/python/file.py
  4. Save it and run that script.bat. It will automatically run you file.py from here. You can place script.bat in your preferred place like in desktop.

14 Comments

I didnt get, to open that bat file wouldn't I have to navigate to whereever that batfile is saved? will typing script.bat from any directory work?
No, that's why I am suggesting you to save bat file in desktop. And this is the alternative approach, I strongly suggest first approach!
Why Desktop ? Desktop isn't my default command_prompt, or powershell directory.
In your PC isn't desktop starts at first? Or is there any other thing?
desktop isnt't the default powershell or command prompt . We dont use mouse. only commandline. We can run only commands, not click anything. default is C:\Users\hp>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.