Hi everybody. I've created a .py file with a simple function in it. Now i want to call that function from linux shell scripting. How should i do that? please help me, thank you.
Niloofar24
15
Posting Whiz
Recommended Answers
Jump to PostCan you post your script? It depends on what you want to do. For example, here is a function which prints hello world
#!/usr/bin/env python # -*-coding: utf8-*- # This is file hello.py # Line 1 above tells the linux shell that this # program must be …
Jump to PostThen the script must take an argument which is the function name. The best thing to do is to use the argparse module to parse the command line, something like
#!/usr/bin/env python3 # -*-coding: utf8-*- def sayhi(): print('hi everybody!') if __name__ == "__main__": import argparse parser = …
Jump to PostIf you saved your file as hello.py somewhere Python looks for, do this from the shell:
>>> import hello >>> hello.thefunc()
Jump to PostOr simpler:
>>> execfile("hello.py")
Jump to PostUse argparse ! It is by far the best way to do this
#!/usr/bin/env python3 # -*-coding: utf8-*- def sayhi(): print('hi everybody!') def goodbye(): print('see you soon!') if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description='Execute a function') parser.add_argument('funcname', help='name of function to execute', metavar='FUNCNAME', choices=['goodbye', 'sayhi']) …
All 20 Replies
Gribouillis
1,391
Programming Explorer
Team Colleague
Niloofar24
15
Posting Whiz
Gribouillis
1,391
Programming Explorer
Team Colleague
sneekula
969
Nearly a Posting Maven
sneekula
969
Nearly a Posting Maven
sneekula
969
Nearly a Posting Maven
Slavi
94
Master Poster
Featured Poster
Niloofar24
15
Posting Whiz
Niloofar24
15
Posting Whiz
Slavi
94
Master Poster
Featured Poster
Niloofar24
15
Posting Whiz
Niloofar24
15
Posting Whiz
Slavi
94
Master Poster
Featured Poster
Niloofar24
15
Posting Whiz
sneekula
969
Nearly a Posting Maven
Niloofar24
15
Posting Whiz
sneekula
969
Nearly a Posting Maven
sneekula
969
Nearly a Posting Maven
Gribouillis
1,391
Programming Explorer
Team Colleague
Niloofar24
15
Posting Whiz
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.