0

How to call a function and print from other program?

I've a textfile.txt file :

"mouse"
"monitor"
"keyboard"
"wire"

My script firstprogram.py :

def call():
  with open('textfile.txt','r') as f:
    input_file = f.readlines()
  for line in input_file:
   print(line)

My another script secondprogram.py :

 from firstprogram import call
 print("hi")
 call()

This is my expected output:

 hi
 mouse
 monitor
 keyboard
 wire
  • 1
    Possible duplicate of [What is the best way to call a script from another script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script) – 4oby Mar 27 '19 at 12:27
  • i m new to coding pls explain with my coding...pls help me sir/madam – Python_beginner Mar 27 '19 at 12:28
  • you need to learn how to import sir./madam, not much we can do just google it and learn how to do it. (There is an easy and dirty way with sys.path.append, but stay away from that and learn the __init__.py way) – E.Serra Mar 27 '19 at 12:35

1 Answers1

0

here you go: First you define the function in firstprogram.py

def call():
   print("I am call")
#save as  firstprogram.py

in the second program secondprogram.py , just call it like this:

from firstprogram import call
call()
MEdwin
  • 2,940
  • 1
  • 14
  • 27