why does string work in command prompt but not in python

This is the place for queries that don't fit in any of the other categories.

why does string work in command prompt but not in python

Postby metulburr » Thu Apr 24, 2014 4:01 am

goal:
I am trying to launch a game from the windows 8 store from steam.

problem:
However windows 8 apparently will not let you just run the executable from windwos 8 store within steam. It has to be ran from the metro. ....I hate windows. I thought this was bogus until i tried to link the exe as a non steam game, and it did fail to launch the game from within steam, but not from metro.

solution:
i found a way to run the game from command line, thus my first thought was to: make a script to subprocess the same command, compile it with py2exe, and just link that exe to steam to launch the game from within steam. However, that command ran within the command prompt works and starts up the game, but the same string ran in subprocess gives me a popup with the error:
Code: Select all
\\
the network path was not found


Code: Select all
#appname
#HKEY_CURRENT_USER > Software > Classes > Extensions > Contractld > Windows.Protocol >Packageld >{APPNAME} > ActivableClassId  > CustomProperties > {name value}

import subprocess
name = 'xboxliveapp-1297292137'
cmd = r'C:\Windows\System32\cmd.exe /c start "" "{}:"'.format(name)
print(cmd)
subprocess.Popen(cmd.split())


the path in the string is empty, but i am not sure why it works in command in any directory, but not in python subprocess. If i add a path to the empty string in the command it just open a file manager in that directory.
Code: Select all
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\metul_000>C:\Windows\System32\cmd.exe /c start "" "xboxliveapp-12972921
37:"

C:\Users\metul_000>cd C:\

C:\>C:\Windows\System32\cmd.exe /c start "" "xboxliveapp-1297292137:"

C:\>

each time launching the game


EDIT:
I tried c++ modifying the cmd path and it works, the same modification to python though gives the old error
Code: Select all
#include <iostream>
#include <cstdlib>

int main(){
   std::string name = "xboxliveapp-1297292137";
   std::string cmd = "cmd.exe /c start \"\" \""+ name +":\"";
   std::cout << cmd << std::endl;
   system(cmd.c_str());
}

Code: Select all
C:\Users\metul_000>g++ test.cpp -o test

C:\Users\metul_000>test.exe
C:\Windows\System32\cmd.exe /c start "" "xboxliveapp-1297292137:"

C:\Users\metul_000>


And this just launches the game, in steam or not. So why does python's subprocess have a problem with it?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: why does string work in command prompt but not in python

Postby 7stud » Thu Apr 24, 2014 4:50 am

What happens when you try: shell=True?

17.5.1.1. Frequently Used Arguments
To support a wide variety of use cases, the Popen constructor (and the convenience functions) accept a large number of optional arguments. For most typical use cases, many of these arguments can be safely left at their default values. The arguments that are most commonly needed are:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.


The program you are trying to execute is cmd.exe.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: why does string work in command prompt but not in python

Postby metulburr » Thu Apr 24, 2014 5:00 am

I get the same results with passing with argument shell=True to Popen()

I also tried just cmd.exe without the path, the same with its path added to the emtpy string in the command, the same with the path to cmd.exe
Code: Select all
C:\Windows\System32\cmd.exe /c start "" "xboxliveapp-12972921
37:"

Code: Select all
cmd.exe /c start "" "xboxliveapp-12972921
37:"

Code: Select all
C:\Windows\System32\cmd.exe /c start "Windows\System32" "xboxliveapp-12972921
37:"

Code: Select all
cmd.exe /c start "Windows\System32" "xboxliveapp-12972921
37:"

the first two gives mt the // error from before, and hte last two just opens in aa file manager the directory in C:\Windows\System32
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: why does string work in command prompt but not in python

Postby 7stud » Thu Apr 24, 2014 5:06 am

Can you get Popen() to work when passing it a list for args?
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: why does string work in command prompt but not in python

Postby metulburr » Thu Apr 24, 2014 5:13 am

you mean instead of splitting the cmd string, just inserting it as a list?
Code: Select all
subprocess.Popen(['C:\Windows\System32\cmd.exe', '/c' ,'start' ,'""', '"{}:"'.format(name)])


I get the same results with or without shell=True
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: why does string work in command prompt but not in python

Postby 7stud » Thu Apr 24, 2014 5:15 am

In your case, it doesn't look like you need to use shell=True:

The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: why does string work in command prompt but not in python

Postby stranac » Thu Apr 24, 2014 6:25 am

You are not supposed to be passing quotes to Popen.
This should work:
Code: Select all
subprocess.Popen(['C:\Windows\System32\cmd.exe', '/c' ,'start' ,'', '{}:'.format(name)])


Using shlex.split() would probably also work, but I prefer to just create the list myself.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: why does string work in command prompt but not in python

Postby metulburr » Thu Apr 24, 2014 7:30 am

You are not supposed to be passing quotes to Popen.

ohh,ok that works.

I assumed the command required the double quotes, but i guess it does not. Thanks.

It actually appears the empty quotes were not required. the quotes wrapped around the name is what was causing the hassle. What is wrong with just writing your command in a variable as a string, and splitting it for Popen's first argument?

Code: Select all
cmd = r'C:\Windows\System32\cmd.exe /c start {}:'.format(name)
subprocess.Popen(cmd.split())

as this works
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: why does string work in command prompt but not in python

Postby stranac » Thu Apr 24, 2014 1:23 pm

metulburr wrote:What is wrong with just writing your command in a variable as a string, and splitting it for Popen's first argument?

Code: Select all
cmd = r'C:\Windows\System32\cmd.exe /c start {}:'.format(name)
subprocess.Popen(cmd.split())

as this works

It works in this case.
If a part of the command contained spaces, cmd.split() would separate it into multiple strings, making the command not work.

So in general, you should either create the list manually or use shlex.split()
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: why does string work in command prompt but not in python

Postby metulburr » Thu Apr 24, 2014 2:25 pm

ah didnt even think of it... I got so acccustomed to creating directory names aand files names in the format of the_program_name, i forgot that sometimes in windows you cant avoid it.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: why does string work in command prompt but not in python

Postby 7stud » Thu Apr 24, 2014 5:56 pm

I also have to ask, why in this day and age is anyone using backslashes in path names?
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: why does string work in command prompt but not in python

Postby metulburr » Thu Apr 24, 2014 7:41 pm

I also have to ask, why in this day and age is anyone using backslashes in path names?

i used it soley because its a windows 8 script and i just copied and pasted the command from the web without changing them to forward slashes. If there was a hint in the fact that it would ever run on linux, i would of used os.path.join(), os.sep, or forward slash, etc.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests