0

How do I find out the basename of the file in this case?

>>> from os.path import basename
>>> basename('C:\\test.exe --help')
'test.exe --help'

Result should be just test.exe without --help or any other arguments.

fnkr
  • 9,428
  • 6
  • 54
  • 61
  • `re.match(r'(.*?)\s-', basename('C:\\test.exe --help')).group(1)` – Avinash Raj May 29 '15 at 09:04
  • What is the use case of this? Since you are typing your command with parameters you might as well not type the parameters so that basename work as intended. Otherwise please explain how does your python program get this command with parameters? Using basename-regular-expression acrobatics is a sign that something can be made simpler elsewhere. – dopstar May 29 '15 at 09:15
  • The user specifies a command (the given string) and I run it using `subprocess.Popen`. In addition I want to log the name of the executed file (`test.exe`) to log (something like `sucessfully called test.exe`). The solution should work with any possible input that `subprocess.Popen` can handle. – fnkr May 29 '15 at 09:19

3 Answers3

1

There is shlex module that mimics behaviour of a Unix shell (but since command.com used to mimic some of its features, it should work too). It'll also will tolerate a quotes (but note that I used raw strings in example):

>>> import shlex
>>> print shlex.split(r'C:\\test.exe --help')
['C:\\test.exe', '--help']
>>> print shlex.split(r'"C:\\test.exe" --help')
['C:\\test.exe', '--help']
>>> print shlex.split(r'"C:\\Program Files\\test.exe" --help')
['C:\\Program Files\\test.exe', '--help']

So take first string returned from shlex.split, and pass to basename.

If you want to get rid of treating backslashes \ as escape sequences, you should construct shlex object explicitly:

>>> from shlex import shlex
>>> lex = shlex('C:\\test.exe --help')
>>> lex.whitespace_split = True
>>> lex.escape = ''
>>> list(lex)
['C:\\test.exe', '--help']
myaut
  • 11,174
  • 2
  • 30
  • 62
1

Well the problem is that, at least on Linux, 'test.exe --exe' is a valid filename. So that is the reason why python does not try to clean filenames from 'parameters'. I looked at windows docs and it looks like you also make file named 'test.exe --exe'. So, it really depends on what you are trying to achieve.

Also have a look at this: What is the most correct regular expression for a UNIX file path?

You should then probably check if file exists, if it does not then use regular expression or the shlex module to strip the parameters...

Visgean Skeloru
  • 2,237
  • 1
  • 24
  • 33
  • That's not exactly true. `test.exe --exe` is also a valid filename on Windows. Running `test.exe --exe` both on Linux and Windows would open `test.exe` not `test.exe --exe` unless you quote or escape it. There is not difference between Windows and Linux at this point. The only difference I found is that `test.exe/test` (no space between `test.exe` and `/test`) would call `test.exe` with `/test` as argument on Windows but not on Linux, however I think that happens rare enough to ignore it in my case. – fnkr May 29 '15 at 10:05
  • test.exe\ --exe would be considered filename in bash, test.exe --exe would not. – Visgean Skeloru May 29 '15 at 10:56
  • Like I said, escape (`test.exe\ --exe`) or quote (`"test.exe --exe"`). – fnkr May 29 '15 at 11:54
0
import os, shlex
print(os.path.basename(shlex.split(r'"C:\\test.exe" --help')[0]))
fnkr
  • 9,428
  • 6
  • 54
  • 61
coder3521
  • 2,608
  • 1
  • 28
  • 50