python program is not working with sys.args - python

i would like to get at least one sys argument when running python script:
if i am not getting at least one argument program should print missing argument text and if i am passing to pyhton script at least 1 argument so run the function in loop
i am getting an error when i am trying to run it. can someone help me with that?
import os
import sys
from time import sleep
argv = sys.argv[1]
my_script = """ipconfig/all >>c://log//out.txt"""
def func():
os.system(my_script)
if len(sys.argv) <= 1:
print("missing argument")
else:
for i in range(int(sys.argv[1:])):
func()
sleep(2)

Related

How to import other script in same dir?

I am trying to put a few python scripts to scheduled and run in main.py. Those scripts are put in the same folder.
main.py:
import schedule
import time
from test1 import dd
schedule.every(2).seconds.do(dd,fname)
while True:
schedule.run_pending()
time.sleep(1)
test1.py:
def dd(fname):
print('hello' + fname)
dd('Mary')
dd('John')
It run out as those 2 name and name 'fname' is not defined.
How to define the argument at main.py file? If I have more than one def in the script, shall I need to import multiple times in the main.py
and the script that I import at top of main.py, it run once before running the schedule? That mean it will run one while you import it?
You are not defining your fname in main.py so it says name 'fname' is not defined. You are only importing the functions to main.py from test1.py
Here is the modified code:
main.py
import schedule
import time
from test1 import dd
fname="Mary"
schedule.every(2).seconds.do(dd,fname)
while True:
schedule.run_pending()
time.sleep(1)
test1.py
def dd(fname):
print('hello' + fname)
if you want to input more than one string, just simply use a list! Here is the sample code for test1.py:
def dd(fname:list):
for n in fname:
print('hello' + n)
These codes are tested using Python 3.7.7
Your problem is that you are trying to use a function argument as it's own variable. Importing is not the problem here.
Try this:
import schedule
import time
from test1 import dd
schedule.every(2).seconds.do(dd,("Any String",))
while True:
schedule.run_pending()
time.sleep(1)

python - adding a argument to execution script

consider I am having a following code in my bin as follows(filename: emp_dsb):
import sys
from employee_detail_collector.EmpCollector import main
if __name__ == '__main__':
sys.exit(main())
In my command line I will execute the "emp_dsb", so that above code will execute the main function from "employee_detail_collector.EmpCollector"
Code in (employee_detail_collector.EmpCollector) main():
def main():
try:
path = const.CONFIG_FILE
empdsb = EmpDashboard(path)
except SONKPIExceptions as e:
logger.error(e.message)
except Exception as e:
logger.error(e)
Now I need to add some argument here for emp_dsb, that is like "emp_dsb create_emp" should invoke a new set of functionalities for creating a employee, which is also needs to be added in same main()
someone look and let me know your ideas, If not clear let me know so that i will try to make it more clear.
the standard way to use command line arguments is to do this:
import sys
if __name__ == '__main__':
print(sys.argv)
read up on the doc of sys.argv.
then there are fancier ways like the built-in argparse and the 3rd party docopt or click.
I would personally use 'argparse' module.
Here is the link to a dead simple code sample.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)

return value from one python script to another

I have two files: script1.py and script2.py. I need to invoke script2.py from script1.py and return the value from script2.py back to script1.py. But the catch is script1.py actually runs script2.py through os.
script1.py:
import os
print(os.system("script2.py 34"))
script2.py
import sys
def main():
x="Hello World"+str(sys.argv[1])
return x
if __name__ == "__main__":
x= main()
As you can see, I am able to get the value into script2, but not back to script1. How can I do that? NOTE: script2.py HAS to be called as if its a commandline execution. Thats why I am using os.
Ok, if I understand you correctly you want to:
pass an argument to another script
retrieve an output from another script to original caller
I'll recommend using subprocess module. Easiest way would be to use check_output() function.
Run command with arguments and return its output as a byte string.
Sample solution:
script1.py
import sys
import subprocess
s2_out = subprocess.check_output([sys.executable, "script2.py", "34"])
print s2_out
script2.py:
import sys
def main(arg):
print("Hello World"+arg)
if __name__ == "__main__":
main(sys.argv[1])
The recommended way to return a value from one python "script" to another is to import the script as a Python module and call the functions directly:
import another_module
value = another_module.get_value(34)
where another_module.py is:
#!/usr/bin/env python
def get_value(*args):
return "Hello World " + ":".join(map(str, args))
def main(argv):
print(get_value(*argv[1:]))
if __name__ == "__main__":
import sys
main(sys.argv)
You could both import another_module and run it as a script from the command-line. If you don't need to run it as a command-line script then you could remove main() function and if __name__ == "__main__" block.
See also, Call python script with input with in a python script using subprocess.

calling a script from daemon

I am trying to call a script from python-daemon but its not working. this is what i am tying to do, is it correct?
I also want to pass a random argument to that script, currently i have hard coded it
import daemon
import time
import subprocess
import os
def interval_monitoring():
print "Inside interval monitoring"
while True:
print "its working"
# os.system("XYZ.py 5416ce0eac3d94693cf7dbd8") Tried this too but not working
subprocess.Popen("XYZ.py 5416ce0eac3d94693cf7dbd8", shell=False)
time.sleep(60)
print "condition true"
def run():
print daemon.__file__
with daemon.DaemonContext():
interval_monitoring()
if __name__ == "__main__":
run()
If you didn't make XYZ.py executable and added #!/usr/bin/env python in the top line, you need to call it via python, rather than directly. So your line would be something like this:
subprocess.check_output(["python", "XYZ.py", "5416ce0eac3d94693cf7dbd8"])

Using sys.argv from another .py file - python

I have a file (test.py) that receives sys.argv from the console/bash:
import sys
def main():
ans = int(sys.argv[1])**int(sys.argv[1])
with open('test.out', 'w') as fout:
fout.write(str(ans))
if __name__ == '__main__':
main()
Usually, I could just do $ python test.py 2 to produce the test.out file. But I need to call the main() function from test.py from another script.
I could do as below in (call.py) but is there any other way to run pass an argument to sys.argv to main() in `test.py?
import os
number = 2
os.system('python test.py '+str(number))
Please note that I CANNOT modify test.py and I also have a main() in call.py which does other things.
You can use your program as it is. Because, irrespective of the file invoked by python, all the python files will get the command line arguments passed.
But you can make the main function accept sys.argv as the default parameter. So, main will always take the sys.argv by default. When you pass a different list, it will take the first element and process it.
test.py
import sys
def main(args = sys.argv):
ans = int(args[1])**int(args[1])
with open('test.out', 'w') as fout:
fout.write(str(ans))
call.py
import sys, test
test.main()
Write that like:
import sys
def main(num):
ans = int(num)**int(num)
with open('test.out', 'w') as fout:
fout.write(str(ans))
if __name__ == '__main__':
main(sys.argv[1])
so that your main() function doesn't have to know about sys.argv - it just handles the parameters being passed in to it.
Without modifying test.py you can still run it just as you have it, just do call.py:
import test
test.main()
then $ python call.py ARG will still work. Since you've already imported sys in test, you don't need to reimport it unless you want to use sys in call.py. Note that sys.argv[0]=='call.py' not test.py if use test through call.
Create a function to do your calculation and file writing, which can be called from any other module:
power.py:
import sys
def power(arg):
ans = arg ** arg
with open('test.out', 'w') as fout:
fout.write(str(ans))
if __name__ == '__main__':
power(int(sys.argv[1]))
other.py:
import power
power.power(2)

Categories