0

I created a custom command scan for the windows terminal which executes a python script.

@echo off
python "my_scripts\scan.py"

For initial tests, I have written this fabulous script.

import sys

print(f"The name of the script is: { sys.argv[0] }")

When I type scan into the shell, as for as I know, this should just print: The name of the script is: scan.

But instead, the output I am getting is: The name of the script is: my_scripts\scan.py.

It´s very obvious what the issue is, but I don´t know how to fix this.

Sebastian
  • 17
  • 5
  • 4
    I'm not sure what exactly you want to fix. The value of `sys.argv` is determined by how the script is called. If you only want the final path component, use something like `os.path.basename` to extract it. – chepner Jun 05 '21 at 20:20
  • 1
    `python.exe` is started with `my_scripts\scan.py` and this string is passed to Python script as argument 0. So why do you think the Python script should output `scan` as used by you to start the batch file? Please open a [command prompt](https://www.howtogeek.com/235101/), run `call /?` and read the output help. `echo %0` as second command line in the batch file would output `scan` while `echo %~nx0` would output the batch file name with file extension and `echo "%~f0"` the full qualified batch file name. See also [this batch file question](https://stackoverflow.com/questions/12141482/). – Mofi Jun 05 '21 at 20:27
  • @chepner I didn´t know that, thanks for explaining. – Sebastian Jun 05 '21 at 20:36
  • @Mofi Thanks. I thought that `sys.argv[]` would be whatever I type into the command line to initiate the script. I missinterpreted the [official descritption](https://docs.python.org/3/library/sys.html#sys.argv) – Sebastian Jun 05 '21 at 20:46
  • I suggest to use as second command line in batch file: `python "%~dp0my_scripts\scan.py" %*` Then all arguments passed to the batch file with exception of argument 0 (string used to start the batch file) are passed by Windows command processor (`cmd.exe`) to Python (`python.exe`) which pass the arguments further to the Python script. So first argument string passed to the batch file can be accessed in Python script with `sys.argv[1]`. – Mofi Jun 06 '21 at 08:55
  • The Unix command interpreter *is* a shell. There are many different shells to choose from (`bash`, `dash`, `zsh`, `ash`, `ksh`, `fish`, etc), and all of them are programs that run in a terminal emulator. (The program executed by a terminal emulator does not *have* to be a shell, but shells are, by far, the most commonly executed programs.) – chepner Jun 06 '21 at 14:02

1 Answers1

1

You can use Path.stem to get the name of a file without the extension

__file__ is the full path to the current file which you can pass to Path

from pathlib import Path

print(f"The name of the script is: {Path(__file__).stem}")

You can also pass sys.argv[0] instead if you want to

from pathlib import Path
import sys

print(f"The name of the script is: {Path(sys.argv[0]).stem}")
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50
  • Thanks for your answer. I am not really interested in the file name itself and rather like to be able to read the commands I typed into the terminal. – Sebastian Jun 05 '21 at 20:49
  • You can pass sys.argv[0] to Path instead – Iain Shelvington Jun 05 '21 at 20:51
  • @Sebastian, ...granted, this is a bit different on Windows, but on UNIX-y systems the original string typed in the terminal _is no longer available_ when an external executable is started. Yes, the shell will pick an `argv[0]` element to put in the array of strings it passes to the child process, but there's no obligation for that to directly correlate with what the user typed. Remember, the shell is given a single string, the argv is an array of individual strings; so they're two different, incompatible representations. – Charles Duffy Jun 05 '21 at 21:04
  • @Charles Duffy, oh yeah that makes total sense. So I would have to have the batch file pass those values, I assume. – Sebastian Jun 05 '21 at 21:13