0

I am currently attempting to pass a variable from my C# winforms application to my Python executable through process.start(). The script uses shutil to duplicate and rename a separate python file, the file will be renamed with respect to a variable (var, c# variable)...

  • I want to pass the "current" text-box value of my winforms application to my Python script and run into a name error on my python script. In my script, after clicking a button ,through openFileDialog, I select a excel sheet file in the FileDialog and the full path to the file is pasted in a textbox, "Textboxpath." Here I want to pass the textbox value (the Textboxpath value) of my winforms application to my Python script.

My issue is defining the C# variable current value or value to my Python script. My windows form application runs perfectly with the current script though when I attempt to run my Python script and pass the C# variable through ".Arguments", my Python file returns with "NameError: name 'Textboxpath' is not defined." I have attempted to rewrite the process.start() function including the variable in my python script there has been no success to defining the variable, any help would be very appreciated!

**C#:**
...
#script for defining openFileDialog variable and using OpenFileDialog goes here
Textboxpath.Text = openFileDialog.FileName; #prints file (excel workbook) directory path to text box
...
string var;
var = Textboxpath.Text;
ProcessStartInfo StartInfo
    = new ProcessStartInfo(@"C:\directorytask\dist\modifyfest.exe");
StartInfo.FileName = "C:\\directorytask\\dist\\modifyfest.exe";
StartInfo.Arguments = var;
Process.Start(StartInfo);

**Python script: modifyfest.exe** #packaged with pyinstaller, --onefile
import os
import sys
import shutil

x = var
f = x - '.xlsx'
l = f - 'C:\directorytask'
k = '.py'
y = 'test_'
z = y + l +k
#duplicating/renaming python file
original = 'C:/directorytask/test_five.py' #original python file
target = 'C:/directorytask/' + z   #original python file being duplicated with name z
shutil.copyfile(original, target)

**Error:**

Traceback <most recent call last>:
  File "modifyfest.py", line 5, in <module>
NameError: name 'Textboxpath' is not defined
[34652] failed to execute script modifyfest
Bussarin
  • 11
  • 6
  • In C# [`var` is a keyword](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var), and your variables cannot be named the same as keywords (except if your prefix them with [`@`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/)). You'll need to replace everywhere where you are using `var` as the name with something else, like `textboxpath` – MindSwipe Jul 16 '20 at 05:06
  • thank you for the response! I just attempted this, no change sadly... I switched "var" with "textboxa" in my Winforms application and python script; I received the same error "NameError: name 'textboxa' is not defined" – Bussarin Jul 16 '20 at 05:19
  • Now the problem becomes this: How is Python supposed to know that a variable named `textboxa` is being passed to it? This can be done in multiple ways, but the easiest would probably be to use [ArgumentParse](https://stackoverflow.com/a/1009864/9363973) or [sys.argv](https://stackoverflow.com/a/1009879/9363973) as you are already passing `textboxa` as an argument to python – MindSwipe Jul 16 '20 at 05:23
  • 1
    @MindSwipe you can actually use 'var' as a variable name. They probably did not want to break old .net 2 code when they introduced it. But that does not mean it is a good idea. – JonasH Jul 16 '20 at 07:33
  • tyvm I added sys.argv and it runs perfect :) – Bussarin Jul 16 '20 at 16:55

1 Answers1

0

I added the parser! This is how the Python script looks now, runs perfect...

**answer:**
import os
import sys
import shutil
from pathlib import Path

def parse(p):
    q = p
    return q

x = parse(sys.argv[1]) #imports first argument sent by c#, I attempted sys.argv[0] instead and it returned the first line of my c# ProcessStartInfo list, file name...
p = Path(x).stem
k = '.py'
y = 'test_'
z = y + p +k

original = 'C:/directorytask/test_five.py' #retailer specific duplicated task
target = 'C:/directorytask/' + z   #task being created
shutil.copyfile(original, target)
Bussarin
  • 11
  • 6