-1

So I want to execute a python file which is in my program files folder using visual studio C project.(FYI using the exe file). I know system function can execute a command. I want to say to my command that go to the python folder in appdata and then run that a python file which is in my program file folder.

I tried the following way

char cmd[] = "C:\Users\%user%\AppData\Local\Programs\Python\Python37" "C:\Program Files (x86)\tool\tool.py";
system(cmd);

But it's giving me an error filename,directory name incorrect. Also it's an application so it should be user specific. how to i replace %user% with the actual username.

The second approach I tried is to set the python as environment variable. and then run

char cmd[] = "python " "C:\Program Files (x86)\tool\tool.py";

Since there is a space between program and files. the error says cannot find C:\Program. how do i say the compiler to include space as a part of the directory?

Bodo
  • 9,287
  • 1
  • 13
  • 29
Peace9795
  • 11
  • 6
  • 1
    Any folder/file with spaces in the name will need to be quoted/escaped like `"C:\'Program Files (x86)'\tool\tool.py"` – C.Nivs Feb 13 '20 at 16:11
  • Thanks for replying. it didn't work still the same error – Peace9795 Feb 13 '20 at 16:15
  • So I tried this `char cmd[] = "python " "C:\"Program " "Files " "(x86)\\Tool\tool.py";` The command line is giving me an error `python: can't open file 'C:Program Files (x86)\Tool\tool.py': [Errno 2] No such file or directory` The file is present over there tho – Peace9795 Feb 13 '20 at 16:34
  • Perhaps escaping the spaces, perhaps via a '\' Or perhaps via 'ch' to the 'c"\Program Files (x86)\tool\tool.py' etc. however, the easiest way is to copy the `tool.py` file to the local directory where your program is residing. Then the statement becomes: `char cmd[] = "python tool.py";` – user3629249 Feb 13 '20 at 16:36
  • Changing the file directory is not an option for me. It's a software which has all the files in that folder in order to run it. Is it possible to do it without changing the location ? – Peace9795 Feb 13 '20 at 16:40
  • regarding: `"C:\Users\%user%\AppData\Local\Programs\Python\Python37"` why would you have a separate copy of the executable 'Python37' for each user? – user3629249 Feb 13 '20 at 16:41
  • It's a software which should work on a different computer as well. So if i change my computer the user is going to change right ? I have a batch file which can do the work easily. The batch file contains this command. `%systemdrive%%homepath%\AppData\Local\Programs\Python\Python37\python "C:\Program Files (x86)\Tool\tool.py" Basically i want to execute this same command in my C project. – Peace9795 Feb 13 '20 at 16:45
  • 4
    Try `char cmd[] = "C:\\Users\\%user%\\AppData\\Local\\Programs\\Python\\Python37 \"C:\\Program Files (x86)\\tool\\tool.py\"";` or `char cmd[] = "%systemdrive%%homepath%\\AppData\\Local\\Programs\\Python\\Python37\\python \"C:\\Program Files (x86)\\Tool\\tool.py\"";` – Bodo Feb 13 '20 at 16:47
  • @Bodo Thanks. your second solution worked fine :D. So now i dont have to set python as an environment variable. Thank you very much – Peace9795 Feb 13 '20 at 16:56
  • suggest reading: [building paths in python](https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f) – user3629249 Feb 13 '20 at 16:58
  • 2
    @user3629249 The question is about running a Python program from C, so the Python solution to construct a directory path does not apply. It is not necessary to use backslashes, Windows also accepts forward slashes in many places. I think this requires quoting for `cmd`, so it should be possible to use `char cmd[] = "\"%systemdrive%%homepath%/AppData/Local/Programs/Python/Python37/python\" \"C:/Program Files (x86)\\Tool/tool.py\"";` – Bodo Feb 13 '20 at 17:04
  • 2
    suggest reading: [path names with spaces on windows](https://support.microsoft.com/en-us/help/102739/long-filenames-or-paths-with-spaces-require-quotation-marks) – user3629249 Feb 13 '20 at 17:05
  • So the command I'm using which is `%systemdrive%%homepath%\AppData\Local\Programs\Python\Python37\python "C:\Program Files (x86)\Tool\tool.py"` works fine if my file is present in "C:\Program Files (x86)\Tool\tool.py" which is what i want. So the problem is fixed. but when my file is somewhere else apart from that location. It gives me traceback errors. I just want to know why this is happening. Why the location of the file matters coz the command is simply going to the python dir and calling python file from C: dir. The location of the file shouldn't matter tho right ? – Peace9795 Feb 13 '20 at 21:02
  • @C.Nivs **that won't work** because cmd doesn't remove quotes, and [single quote are not used for quoting in cmd](https://stackoverflow.com/q/24173825/995714). You must quote the whole parameter in `""` – phuclv Feb 14 '20 at 02:05
  • @user3629249 the escape character in cmd is `^`, not \ so that won't work either – phuclv Feb 14 '20 at 02:19

1 Answers1

1

Let's talk.

First I don't think that

char cmd[] = "C:\Users\%user%\AppData\Local\Programs\Python\Python37" "C:\Program Files (x86)\tool\tool.py";

does what you think it does. When you put two strings like that together the compiler treats it as one long string and you will not get a space between the "...Python37" and "C:...". Better to just make that one string and put your space delimiter in.

For example:

char cmd[] = "C:\Users\%user%\AppData\Local\Programs\Python\Python37 C:\Program Files (x86)\tool\tool.py";

The next issue is that C strings reserve the '\' (back-slash) character as an "escape" character. There's a history lesson in that terminology but that's for another day. The important part is that it allows you to put characters into strings that you would not normally be able to. Examples are things like tabs (\t), newlines (\n), etc. Whenever the compilers sees a "\" it will be expecting another character to complete the "escape sequence". If you actually want a backslash you have to put in two.

For example:

char cmd[] = "C:\\Users\\%user%\\AppData\\Local\\Programs\\Python\\Python37 C:\\Program Files (x86)\\tool\\tool.py";

Next, you are using an environment variable expansion "%user%". I assume that is defined in your environment (it isn't in mine). You need to be mindful of the environment and you may want to check that things are expanding as you expect. One easy way to do this is using your same code but a different cmd string:

For example:

char cmd[] = "echo %USER% >c:\\mydir\\myoutput";
system(cmd);  

It's useful to put a full path on the redirect to make sure it ends up where you expect it to. Again I'm going to assume that %USER% is correctly defined in your environment.

Next, you are referencing a file path that has a space in it. That might be why you tried to use the quotes the way you did, but in this case that doesn't help you. The system function accepts a string and for the most part doesn't much care what it is. You need something to indicate that the space is part of the file path. This is where those back-slashes can really help you.

For example:

char cmd[] = "C:\\Windows\\system32\\cmd.exe /K dir \"C:\\Program Files (x86)\"";
system(cmd);

That should open a DOS/CMD window on your desktop, execute a dir of "C:\Program Files (x86)" then leave the cmd shell open. Sometimes leaving the shell open in this way can be handy to see what the default env is.

So putting it altogether your program should look something like this:

int main() {
char cmd[] = "C:\\Users\\%user%\\AppData\\Local\\Programs\\Python\\Python37 \"C:\\Program Files (x86)\\tool\\tool.py\"";
system(cmd);
}
Dweeberly
  • 4,668
  • 2
  • 22
  • 41
  • @usr2564301 You are most correct, thank you for pointing that out, I found another spot I'd missed them also. Hopefully, I've got all :-) – Dweeberly Feb 13 '20 at 22:16
  • [cmd.exe is not DOS](https://superuser.com/q/451432/241386). They're very different things with different syntaxes although some commands work in both – phuclv Feb 14 '20 at 02:07
  • and to get/expand the environment variable use [`GetEnvironmentVariable()`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable)/[`ExpandEnvironmentStringsW()`](https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsw). Don't save it to a file – phuclv Feb 14 '20 at 02:09
  • The prob is solved but, So the command I'm using which is %systemdrive%%homepath%\AppData\Local\Programs\Python\Python37\python "C:\Program Files (x86)\Tool\tool.py" works fine if my file is present in "C:\Program Files (x86)\Tool\tool.py" which is what i want. So the problem is fixed. but when my file is somewhere else apart from that location. It gives me traceback errors. I just want to know why this is happening. Why the location of the file matters coz the command is simply going to the python dir and calling python file from C: dir. The location of the file shouldn't matter tho right ? – Peace9795 Feb 14 '20 at 13:21
  • You may wish to post more information as to the nature of the error. You might try prepending your string with "echo" and appending a redirect to a file. Then try the command exactly as it appears in the file to check to see if it works as you expect it to. – Dweeberly Feb 14 '20 at 15:34