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

问题描述 投票:0回答:1

我想调用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'))那样打电话

python python-3.x callback
1个回答
0
投票

first.py的内容将是

func_first(...):
   #do_something

second.py的内容将是

from first import func_first
func_second(...):
    func_first(...)
© www.soinside.com 2019 - 2024. All rights reserved.