Author Topic: [Python] variable question  (Read 4838 times)

0 Members and 1 Guest are viewing this topic.

Offline Corrupted_Fear

  • Knight
  • **
  • Posts: 336
  • Cookies: 34
  • Is dangerous to go alone! Take this! @xxxx[{:::::>
    • View Profile
[Python] variable question
« on: February 21, 2013, 04:39:20 am »
This sounds really stupid, but I honestly am not even sure what exact words I am supposed to use to google this topic, so after searching with no good results, I ask here. I am writing a program in python, and I want to run in from a prompt as "python program.py variable", and have the variable given saved and run through a function.


Example:


def function (variablefromshell):
        print (variablefromshell)


prompt command: python program.py 5
shell output: 5


I am used to using input ("> ") to set user defined variables, but I'm not quite sure how accomplish what I want to do above.Your time and assistance would be greatly appreciated :)

by | Angel | Devil |

"Welcome to le trove that is my home. Welcome to EvilZone." -- DeepCopy

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: [Python] variable question
« Reply #1 on: February 21, 2013, 04:42:39 am »
The sys module can parse arguments from the command line to your program.

Or:

http://docs.python.org/library/optparse.html

Or:

http://docs.python.org/library/argparse.html

Or:

http://docs.python.org/2/library/getopt.html

Its called parsing :)
And thats python, too many options too many modules :P

*edit*
Read this: http://www.python.org/dev/peps/pep-0389/
« Last Edit: February 21, 2013, 04:47:20 am by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline Corrupted_Fear

  • Knight
  • **
  • Posts: 336
  • Cookies: 34
  • Is dangerous to go alone! Take this! @xxxx[{:::::>
    • View Profile
Re: [Python] variable question
« Reply #2 on: February 21, 2013, 04:49:17 am »
perfect, that is exactly what I needed, thanks so much!


edit: to share my 6 lined script. Rather impractical but I was bored and needed something faster then "taskkill /f /im notepad.exe /t". A batch file would have done fine, but why use batch when you have python. The fruits of my half hour below:

Code: (python) [Select]
#!/usr/bin/env



import os
import argparse


parser = argparse.ArgumentParser()
parser.add_argument("kill", help="kills the selected process")
args = parser.parse_args()
os.system ("TASKKILL /F /IM " + args.kill + ".exe /T")

Staff note: Use code tags, it helps =)

C_F edit: ty, looks better :)
« Last Edit: February 21, 2013, 09:42:05 pm by Corrupted_Fear »

by | Angel | Devil |

"Welcome to le trove that is my home. Welcome to EvilZone." -- DeepCopy

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Python] variable question
« Reply #3 on: February 21, 2013, 05:45:42 am »
argparse could work. But to answer the question in what most would use:

Code: (python) [Select]
#!/usr/bin/python2

import sys, os

#returns a list of everything that was passed in CLI
arguments = sys.argv

#debug to see what you got
print arguments

#you can then use the list
os.system("TASKKILL /F /IM %s.exe /T" % arguments[1])

####################
#total code
#there is no error checking in this nor your code

import os, sys

#use os.popen to get feedback for what you did
os.system("TASKKILL /F /IM %s.exe /T" % sys.argv[1])

For more info, look up sys.argv

Also, since your using batch, the hashbang (#!/usr/bin/whateve) is not needed. The hashbang is for linux only.
« Last Edit: February 21, 2013, 05:50:24 am by techb »
>>>import this
-----------------------------

Offline jay755

  • /dev/null
  • *
  • Posts: 9
  • Cookies: -1
    • View Profile
Re: [Python] variable question
« Reply #4 on: February 21, 2013, 09:51:19 am »
Most people would use sys for that, neat programmers would use argparse for that :).

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [Python] variable question
« Reply #5 on: February 21, 2013, 10:06:00 am »
Code: (python) [Select]
import sys
from subprocess import call

if len(sys.argv) == 2:
    call(["TASKKILL", "/F", "/IM", sys.argv[1], "/T"])
else:
    print "Please provide process name!"
« Last Edit: February 21, 2013, 10:06:19 am by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Python] variable question
« Reply #6 on: February 21, 2013, 01:33:31 pm »
Most people would use sys for that, neat programmers would use argparse for that :).

argparse uses sys.argv there is no need to call more methods for something like this. Programmers use the simplest solution. Inexperienced people use unneeded code.
>>>import this
-----------------------------

Offline jay755

  • /dev/null
  • *
  • Posts: 9
  • Cookies: -1
    • View Profile
Re: [Python] variable question
« Reply #7 on: February 21, 2013, 01:45:47 pm »
Sounds fair. Inexperienced people use unneeded code indeed.


Sometimes I'd like my code to be maintainable and extendable. argparse gives me a nice api to handle long and complicated command line options. sys does not.


For this short example sys.argv works fine, but learning something which is maintainable in the future when your projects get bigger could be useful.


Jay