I donĀ“t know, how to run a python file (python test.py) with extra stuff, like this:
python test.py "hello world"
python [FILE] [SAY]
What i want:
def something(say):
print(say)
A simple example of using argparse:
import argparse
cmd_parser = argparse.ArgumentParser()
cmd_parser.add_argument('SAY', help= 'The string you want to print on the
terminal')
args = cmd_parser.parse_args()
print(args.SAY)
I found a solution (a long time ago btw):
import sys
sys.argv
Thats it! It returns a list with all arguments:
C:\Programs >>> test.py-t "Hello, World!" start
["test.py","-t","Hello World","start"]
I am unable to get this working .
I have something like this
python executor.py arg1 arg2 arg3
Ant then I have another python script which is not directly called by excecutor.py but by some another script file which is being called in executor.py .Lets it call
script.py
there is a variable name argument in which i want to catch arg1.
How to do do it ?
I am assuming you are doing import script within the executor.py script. If this is the case you have to add to executor.py:
import script
import sys
argument_1 = sys.argv[1] # since [0] is the script name
...
script.yourfunction(argument_1)
assuming script.py and executor.py are in the same folder:
script.py:
def function1(somearg, otherarg):
pass
def function2(moreargs):
pass
and executor.py
import sys
import script
# assign input args; check for valid arguments etc..
arg1 = sys.argv[1]
arg2 = sys.argv[2]
etc...
# call system functions
script.function1(arg1, arg2)
script.function2(arg1)
I want to pass a string of ZPL codes from one python script to another python script. The string becomes malformed when used in the second script. How can I pass a string literal as an argument to another python script without it being malformed?
Original String
^XA^FO20,20^BQ,2,3^FDQA,001D4B02107A;1001000;49681207^FS^FO50,50^ADN,36,20^FDMAC: 001D4B02107A^FS^FO50,150^ADN,36,20^FDSN: 1001000^FS^FO50,250^ADN,36,20^FDCode: 49681207^FS^XZ
Malformed string
XAFO20,20BQ,2,3FDQA,001D4B02107A;1001000;49681207FSFO50,50ADN,36,20FDMAC:
Code where I call the second script
def printLabel():
label = "^XA"+"^FO20,20^BQ,2,3^FDQA,"+"001D4B02107A;1001000;49681207"+"^FS"+"^FO50,50"+"^ADN,36,20"+"^FD"+"MAC: "+"001D4B02107A"+"^FS"+"^FO50,150"+"^ADN,36,20"+"^FD"+"SN: "+"1001000"+"^FS"+"^FO50,250"+"^ADN,36,20"+"^FD" + "Code: "+"49681207"+"^FS"+"^XZ"
command = "zt320print.py "+label
print command
sys.stdout.flush()
exitCode = os.system(str(command))
Code that receives the argument
if __name__ == "__main__":
zplString = str(sys.argv[1])
print zplString
printZPL(zplString)
If your code needs to be written just as it is (including the rather odd way of stringing together the ZPL code, and calling a separate script via a shell intermediary, and the avoidance of subprocess, for that matter), you can resolve your issue with a few small adjustments:
First, wrap your code string in double-quotes.
label= '"^XA'+"^FO20,20^BQ,2,3^FDQA,"+"001D4B02107A;1001000;49681207"+"^FS"+"^FO50,50"+"^ADN,36,20"+"^FD"+"MAC: "+"001D4B02107A"+"^FS"+"^FO50,150"+"^ADN,36,20"+"^FD"+"SN: "+"1001000"+"^FS"+"^FO50,250"+"^ADN,36,20"+"^FD" + "Code: "+"49681207"+"^FS"+'^XZ"'
Second, make sure you're actually calling python from the shell:
command = "python script2.py "+label
Finally, if you're concerned about special characters not being read in correctly from the command line, use unicode_escape from codecs.decode to ensure correct transmission.
See this answer for more on unicode_escape.
# contents of second script
if __name__ == "__main__":
from codecs import decode
import sys
zplString = decode(sys.argv[1], 'unicode_escape')
print(zplString)
Now the call from your first script will transmit the code correctly:
import sys
import os
sys.stdout.flush()
exitCode = os.system(str(command))
Output:
^XA^FO20,20^BQ,2,3^FDQA,001D4B02107A;1001000;49681207^FS^FO50,50^ADN,36,20^FDMAC: 001D4B02107A^FS^FO50,150^ADN,36,20^FDSN: 1001000^FS^FO50,250^ADN,36,20^FDCode: 49681207^FS^XZ
Some demo code:
import sys
if __name__ == "__main__":
for i, arg in enumerate(sys.argv):
print("{}: '{}'".format(i, arg))
when called like
python test.py ^this^is^a^test
it gives
0: 'test.py'
1: 'thisisatest'
when called like
python test.py "^this^is^a^test"
it gives
0: 'test.py'
1: '^this^is^a^test'
Solution: enclose your parameter string in double-quotes, ie
label = '"' + label + '"'
You can put your string inside a double-quotes, or just import the other python script:
a.py
import sys, os
text = "a b c d"
# or '{} {} "{}"'.format("python", "b.py", text)
command = "python b.py \"" + text + "\""
os.system(str(command))
b.py
import sys
if __name__ == "__main__":
first_argument = str(sys.argv[1])
print(first_argument)
Output
a b c d
I am tying a script which will pass argparse arguments to another python
1st script : t.py
import argparse
import subprocess
import os
commandLineArgumentParser = argparse.ArgumentParser()
commandLineArgumentParser.add_argument("-fname", "--fname", help="first name")
commandLineArgumentParser.add_argument("-lname","--lname", help="last name")
commandLineArguments = commandLineArgumentParser.parse_args()
fname = commandLineArguments.fname
lname = commandLineArguments.lname
print "%s\n%s" %(fname,lname)
os.system("python test1.py")
code for test1.py is bellow
import argparse
import os
print "test abc"
I want to pass lname and fname values to test1.py .is their their any way to do that.
in the above code if I ran
python t.py -fname ms lname = dhoni
then the output is
ms
dhoni
test abc
But I want the output to be like bellow
ms
dhoni
ms
dhoni
Try this for test1.py:
from sys import argv
print "%s\n%s" % (argv[1], argv[2])
Hum I don't understand why you are trying to do that, but you really already have all that is required to achieve this task :
First python script (I call it sof.py):
import argparse
import subprocess
import os
commandLineArgumentParser = argparse.ArgumentParser()
commandLineArgumentParser.add_argument("-fname", "--fname", help="first name")
commandLineArgumentParser.add_argument("-lname","--lname", help="last name")
commandLineArguments = commandLineArgumentParser.parse_args()
fname = commandLineArguments.fname
lname = commandLineArguments.lname
print("%s\n%s" %(fname,lname))
command = "python sof2.py {arg1} {arg2}".format(arg1=fname, arg2=lname)
os.system(command)
Second python scrit (sof2.py here)
import argparse
import subprocess
import os
commandLineArgumentParser = argparse.ArgumentParser()
commandLineArgumentParser.add_argument("fname")
commandLineArgumentParser.add_argument("lname")
commandLineArguments = commandLineArgumentParser.parse_args()
fname = commandLineArguments.fname
lname = commandLineArguments.lname
print "%s\n%s" %(fname,lname)
This give me the following execution :
python3 sof.py -fname foo -lname bar
foo
bar
foo
bar
NB : I use python3 but if you have to use python2 this code is still correct, just remove ( and ) around the print
you can modify test1.py to include t.py and directly access the argparse variables.
test1.py
import t.py
print t.fname, t.lname
Change your test1.py to:
import argparse
import os
import sys
print sys.argv
print "test abc"
And call your test1.py from t.py as:
os.system("python test1.py vv gg hh")
Now your args vv gg hh are available in test1.py.
test1.py is the main script calling another script test2.py by passing the same argument list that has been passed to test1.py. I have done following but it reads the sys.argv list as string and parse into multiple arguments and also includes unnecessary [ and ,
test1.py
import os
import sys
argList=sys.argv[1:]
os.system('python another/location/test2.py %s'%(argList))
test2.py
import sys
print(sys.argv[1:])
Call test1.py
python test1.py -a -b -c
output: ['[-a,' ,'-b,', '-c]' ]
Please post if there is a better opti
Use
os.system('python another/location/test2.py %s' % ' '.join(argList))
if the arguments will contain no spaces themselves.
The second program will output
['-a', '-b', '-c']
If your arguments can contain spaces, it might be best to quote them. Use ' '.join("'%s'" % arg.replace("'", "\\'") for arg in ArgList)
in the case if you need to exit test1.py just after calling test2.py
import subprocess
subprocess.Popen( ('python', 'another/location/test2.py') + tuple( sys.argv[1:]) )