【问题标题】:call a function from other/Program python从其他/程序python调用函数
【发布时间】:2019-03-15 03:40:20
【问题描述】:

`第一个程序:first.py

list=["ab","cd","ef"]
for i in list:
    with open("input.txt", "w") as input_file:
        print(" {}".format(i), file = input_file)

预期输出:

ab
cd
ef

但我得到了输出:

ef

第二个程序:second.py

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

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

预期输出:

ef

得到输出:

ef

现在我想直接从 first.py 调用文本文件(input.txt)并在 second.py 中使用它?`如何从其他程序 python 调用函数?

编辑:应用的代码块

【问题讨论】:

  • 嗨!请使用编辑器格式化您的代码,使其可读。
  • 我改变了格式
  • list 是python中的保留字。当您为其分配一个值时,您将失去该词的所有功能。基本上,如果您尝试拨打list(something),您将得到TypeError。不要使用关键字或保留字作为变量名!

标签: python python-3.x function call func


【解决方案1】:

您在for 循环中打开文件,并使用w 作为open 函数的模式参数,它使open 覆盖它打开的文件,这就是为什么您只能得到循环最后一次迭代的输出。

您应该改为在循环外打开文件:

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

【讨论】:

  • 不评论list作为变量的使用?
  • 非常感谢。还有一个问题如何在 second.py 中导入该函数并打电话给我一个解决方案。
【解决方案2】:

first.py 中,像这样更改您的代码。

w 模式用于写操作。在 for 循环的每次迭代中,您都将覆盖最后一个内容并编写新内容。所以input.txt 里面有ef(最后)。

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

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

现在你会得到你所期望的。现在input.txt 将与您的情况不同。

ab
cd
ef

注意:但如果您将第二次运行first.py,它将继续添加,因为a+ 如果文件不存在则创建文件,否则它会追加。 为了更好地运行此代码,请使用 os.path 模块的 exists() 函数。

如果您想调用first.py 中可用的代码,请将其包装在函数中。然后将该函数导入second.py 并调用。

例如

首先确保first.pysecond.py 在同一个目录中。

first.py

def create_file(file_name):
    list=["ab","cd","ef"]
    for i in list:
        with open(file_name, "a+") as input_file:
            print(" {}".format(i), file = input_file)

second.py

from first import create_file

def read_file(file_name):
    # Create file with content
    create_file(file_name)

    # Now read file content
    input_file = open(file_name, 'r')     
    for line in input_file:
        if "ef" in line:
           print(line)

read_file('input.txt')

打开终端,导航到这个目录,运行python second.py

https://www.learnpython.org/en/Module... | https://www.digitalocean.com... |如果您想阅读并尝试如何在 Python 中创建模块/包https://www.programiz.com/pytho... 将是您的帮手。

更新:上面有你在评论中提到的问题,在每次运行时,它都会附加内容。让我们通过对first.py 进行一些更改来修复它,如下所示。

import os

def create_file(file_name):
    l = ["ab", "cd", "ef"]

    if os.path.exists(file_name): # If file `input.txt` exists (for this example)
        os.remove(file_name)      # Delete the file

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

就是这样(如果您遇到困难,请在评论中更新)。

【讨论】:

  • 非常感谢。还有一个问题,如何直接为 second.py 程序调用 input.txt 文件,而不是像这样调用 --> input_file = open('input.txt','r')
  • 如何在 second.py 中导入该函数并打电话请给我一个解决方案。
  • 我已经更新了我的答案,请检查一下,很抱歉迟到了回答您的问题。
  • 非常感谢。当我第一次运行代码时,我得到了正确的输出,但是当我再次运行时,它打印了两次答案,然后我再次运行了它打印三次的代码..我不知道为什么请建议我
  • 好的,知道了。您的意思是在每次运行中,您只需要相同的 o/p。做一件事,检查文件是否存在。如果存在,请将其删除并重新创建。所以在一次运行中,只有列表的内容会被写入文件。在第 2 次会话中,程序将检测到存在并将其删除。它将再次创建与以前相同的内容。只需检查我是否已更新我的答案。我也保留了旧的,以便您可以正确理解其中的区别。
猜你喜欢
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 2013-01-12
  • 1970-01-01
相关资源
最近更新 更多