6 Replies - 5820 Views - Last Post: 01 February 2011 - 11:37 AM Rate Topic: -----

#1 Guest_baddlust*


Reputation:

arguments help

Posted 31 January 2011 - 02:49 PM

I made a program in C++ so i want to use python for calling it.

os.system("D:\\C++ Projects\\basic.exe <arg here> ")


How can i make this working..

I want the arguments to enter in python too when calling the program.

tnx for your time
Is This A Good Question/Topic? 0

Replies To: arguments help

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: arguments help

Posted 01 February 2011 - 01:27 AM

Do you need to use the command line to open it? Maybe you could use subprocess like this:
subprocess.call('D:\\C++ Projects\\basic.exe')

Was This Post Helpful? 0
  • +
  • -

#3 Guest_butthead*


Reputation:

Re: arguments help

Posted 01 February 2011 - 05:51 AM

my $arg="me";
system("\"C:\\Program Files\\c++\" program.exe \"$arg\"\r\n");



To be more specific i want to make this perl script in python.
Was This Post Helpful? 0

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: arguments help

Posted 01 February 2011 - 06:07 AM

To get an argument in a string, you need the string quantifier %s:

arg = raw_input("Enter an argument")                 #Ask the user to enter an argument
os.system("D:\\C++ Projects\\basic.exe  %s ") % arg   



Was that more what you were thinking of?


This is in Python 2.x but the concept is the same for all versions.

This post has been edited by Simown: 01 February 2011 - 06:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_butthead*


Reputation:

Re: arguments help

Posted 01 February 2011 - 06:14 AM

Yeah tnx for the reply but a get an error

Quote

TypeError: unsupported operand type(s) for %: 'int' and 'str'


Any hints?
Was This Post Helpful? 0

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: arguments help

Posted 01 February 2011 - 09:09 AM

To include a number in the formatting you can:

arg = 2
os.system("D:\\C++ Projects\\basic.exe  %i ") % arg 



With the integer qualifier %i. See String Formatting for more information.

If you really want it as a string, you can typecast the argument:

arg = 2
os.system("D:\\C++ Projects\\basic.exe  %s ") % str(arg) 


This post has been edited by Simown: 01 February 2011 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: arguments help

Posted 01 February 2011 - 11:37 AM

I'd recommend using:
arg = 2
os.system("D:\\C++ Projects\\basic.exe  "+str(arg))  



Because the other approach is considered deprecated on the current versions of Python.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1