Buttonbar Parameter an Python Skript übergeben

German support forum

Moderators: white, Hacker, Stefan2

Post Reply
Stiltzkin
Member
Member
Posts: 182
Joined: 2004-09-10, 17:15 UTC

Buttonbar Parameter an Python Skript übergeben

Post by *Stiltzkin »

Versucht wird folgendes:
Über einen Button in der Buttonbar werden das aktuelle Verzeichnis (%P) und das Fenster gegenüber (%T) als Parameter an ein Python Skript übergeben.

Dabei wurde folgendes beobachtet:
  • Bei Pfaden die kein Leerzeichen enthalten funktioniert es.
    Mit %p und %t funktioniert es auch.
    Wenn die Parameter an eine Batchdatei übergeben werden funktioniert es. (%1 bzw %~1)
    Benutzt man "%P %T erkennt Python das als ein einzelnes langes Argument.
Mit "%P" und "%T" funktioniert es aber nicht.


Zum nachvollziehen:
  • Ordner 1: d:\Totalcmd\Misc\Scripts\Python\Test 1 2 3\
    Ordner 2: d:\Totalcmd\Misc\Scripts\Python\Test 4 5 6\
Output mit %P %T (ohne Leerzeichen)

Code: Select all

['test.py', 'D:\\Totalcmd\\Misc\\Scripts\\Python\\', 'e:\\Downloads\\']
1:      D:\Totalcmd\Misc\Scripts\Python\
2:      e:\Downloads\

Output mit "%P" "%T" (Leerzeichen im Pfad)

Code: Select all

['test.py', 'D:\\Totalcmd\\Misc\\Scripts\\Python\\Test 1 2 3" D:\\Totalcmd\\Misc
\\Scripts\\Python\\Test', '4', '5', '6"']
1:      D:\Totalcmd\Misc\Scripts\Python\Test 1 2 3" D:\Totalcmd\Misc\Scripts\Python\Test
2:      4
3:      5
4:      6"



Python code

Code: Select all

import sys

print(len(sys.argv))
print(sys.argv)

arg1=sys.argv[1]
arg2=sys.argv[2]

for x in range(1, len(sys.argv)):
    print('{}:\t{}'.format(x,sys.argv[x]))
TC Button

Code: Select all

TOTALCMD#BAR#DATA
cmd /k python test.py
%P %T
WCMICON2.DLL,65
totalcmd2python
%COMMANDER_PATH%\Misc\Scripts\Python\
Kann das jmd erklären bzw korrigieren. Wie übergebe ich 2 Pfade mit Leerzeichen an ein Pythonskript?

Edit1: gemeint war "%P %T statt "%P %N
Last edited by Stiltzkin on 2017-03-25, 19:25 UTC, edited 1 time in total.
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

TC setzt bei Verwendung von %P%N - in dieser Kombination und Reihenfolge - automatisch Anführungszeichen, in allen anderen Fällen nicht.

Grüße
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
Stiltzkin
Member
Member
Posts: 182
Joined: 2004-09-10, 17:15 UTC

Post by *Stiltzkin »

Ja, das ist mir bekannt. In diesem Fall war das aber nicht relevant - hatte mich nur verschrieben.

Probiert habe ich Kombinationen aus:
  • %P %T
    "%P" "%T"
    \"%P\" \"%T"
    ""%P" "%T""
etc...alles ohne Erfolg.
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

Da ich sehe, dass du das Skript direkt aufrufst: Wie sieht die Dateizuordnung der .py Dateien aus?

Grüße
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
Stiltzkin
Member
Member
Posts: 182
Joined: 2004-09-10, 17:15 UTC

Post by *Stiltzkin »

Ich rufe das Skript inkl. den Paramatern auf über:

Code: Select all

cmd /k python test.py %P %T
python.exe befindet sich im PATH
Letztendlich wird folgendes ausgeführt:

Code: Select all

%windir%\system32\cmd.exe /k d:\Programme\Python\python36-32\python.exe  %COMMANDER_PATH%\Misc\Scripts\Python\test.py %P %T
Im Totalcmd ist *.py an sich nur mit Notepad++ verknüpft.

Image: https://i.imgur.com/R2sQUsP.png

cmd /k benutze ich nur damit die Console offen bleibt.
User avatar
Hacker
Moderator
Moderator
Posts: 13052
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Post by *Hacker »

Stiltzkin,

Code: Select all

"%P\" "%T\"
:?:

Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
Stiltzkin
Member
Member
Posts: 182
Joined: 2004-09-10, 17:15 UTC

Post by *Stiltzkin »

:!:

Super, vielen Dank!
Kannst du kurz erklären 'wie und warum' das so funktioniert?

Wenn ich z.b in der Kommandozeile alles von Hand eingebe
python test.py "1 2 3" "4 5 6"
geht das ohne Escape-Zeichen.
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

Ach, ich hab den Button im OP übersehen. CMD erwartet bei Verwendung von /C und /K das komplette Kommando umgeben von Anführungszeichen, also z.B.

Code: Select all

cmd /k "notepad.exe"
. Bei simplen Kommandos geht's auch ohne, aber gerade bei Verwendung von Parametern muss man aufpassen.

Grüße
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
Hacker
Moderator
Moderator
Posts: 13052
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Post by *Hacker »

Stiltzkin,
With "%P" I think the \ at the end of the path escapes the closing " so it is not considered to be the end of the argument. When we add a \ before the closing " ("%P") the \ at the end of the path escapes our \ (so it becomes a literal \) and it does not touch the " which then signifies the end of the first argument.
Same goes for "%T".

EDIT: Ähh, sorry, kann ich es auf Englisch lassen?

Dalai,
CMD erwartet bei Verwendung von /C und /K das komplette Kommando umgeben von Anführungszeichen
Ähh, nein?
cmd /? wrote:If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:

1. If all of the following conditions are met, then quote characters
on the command line are preserved:

- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()@^|
- there are one or more whitespace characters between the
two quote characters
- the string between the two quote characters is the name
of an executable file.

2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
HTH
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
User avatar
Dalai
Power Member
Power Member
Posts: 9364
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

Da die Parameter %P und %T selbst gequotet sind, werden insgesamt mehr als zwei Quotezeichen benutzt. Also sind nicht alle Bedingungen erfüllt, und daher die äußeren(?) Quotes entfernt. Man müsste sich das im Process Explorer oder Process Hacker genau anschauen, was rauskommt, wo Quotes fehlen. Es ist leider eine sehr komplexe Geschichte, vor allem bei Verwendung verschiedener Programme (hier TC, Python und CMD).

Grüße
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
Hacker
Moderator
Moderator
Posts: 13052
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Post by *Hacker »

Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
Stiltzkin
Member
Member
Posts: 182
Joined: 2004-09-10, 17:15 UTC

Post by *Stiltzkin »

Super, nochmals Danke an euch beide.

Läuft so mit Python, Java, Powershell:

Code: Select all

cmd /k python params.py
"%P\" "%T\"

Code: Select all

cmd /k java -jar params.jar
"%P\" "%T\"

Code: Select all

cmd /k powershell -noexit -file params.ps1
"%P\" "%T\"

Code: Select all

params.bat
"%P" "%T"
Post Reply