我想调用first.py中可用的代码,然后将其包装在函数中。然后将该功能导入second.py并调用

时间:2019-03-15 05:21:40

标签: python python-3.x callback

我想先调用first.py中提供的代码,然后将其包装在函数中。然后将该功能导入second.py并调用。

first.py

list=["ab","cd","ef"]

for i in list:
with open("input.txt", "a+") as input_file:
    print("{}".format(i), file = input_file)

Output:
ab
cd
ef

second.py

input_file = open('input.txt','r')     

for line in input_file:
if "ef" in line:
   print(line)

Output:
ef

我想直接从second.py中的first.py脚本/程序读取文本文件(input.txt)。而不是像(input_filt=open('input.txt,'r')

1 个答案:

答案 0 :(得分:0)

first.py的内容将是

func_first(...):
   #do_something

second.py的内容将是

from first import func_first
func_second(...):
    func_first(...)