简体   繁体   中英

Running a Python program from the command line using only the program name

I'm being asked to make a python program that parses tokens.

The usage is as follows:

$ cat input.txt | program "some text %{0} and %{1S3}" > output.txt

but the best I can manage is:

$ cat input.txt | py program.py "some text %{0} and %{1S3}" > output.txt

or if I make the script executable, remove the file extension, and I'm in the current directory

$ cat input.txt | ./program "some text %{0} and %{1S3}" > output.txt

Is there a way for me to use the first example's style of execution with a python script? Ideally I'd also be able to use it from anywhere, not necessary when pointing at the directory containing the program.

Edit: I've tried this:

Here's what I tried --

$ cd projects/token_parser/
$ ln -s grap /usr/local/bin/grap
ln: failed to create symbolic link '/usr/local/bin/grap': Permission denied
$ sudo ln -s grap /usr/local/bin/grap 
[sudo] password for fosssaintdross: 
$ grap
bash: grap: command not found

You need to make sure the location containing program.py is in the PATH environment variable. Alternatively you can link to it from a path that is already in the list, for example:

ln -s /path/to/program.py /usr/local/bin/program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM