2

`First program: first.py

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

Expected Output:

ab
cd
ef

But i got Output:

ef

Second Program: second.py

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

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

Expected Output:

ef

Got Ouput:

ef

Now i want to call directly the text file(input.txt) from first.py and use it in second.py ?`How to call a function from other program python?

Edit: Applied code blocks

3
  • Hi! Please format your code using the editor so it is readable. Commented Mar 15, 2019 at 3:42
  • i changed the format Commented Mar 15, 2019 at 3:57
  • list is a reserved word in python. When you assign a value to it you lose all of that word's functionality. Basically if you tried calling list(something) you will get a TypeError. DO NOT use keywords or reserved words as variable names! Commented Mar 15, 2019 at 4:49

2 Answers 2

2

You're opening the file in a for loop, and with the w as the mode parameter for the open function it makes open overwrite the file it opens, which is why you only get the output from the last iteration of the loop.

You should open the file outside the loop instead:

with open("input.txt", "w") as input_file:
    for i in list:
        print("{}".format(i), file = input_file)
Sign up to request clarification or add additional context in comments.

2 Comments

No comment on the use of list as a variable?
Thanks a lot. one more question how to import that function in second.py and call please give me a solution.
0

In first.py, change your code like this.

w mode is for write operation. In each iteration of for loop you are overwriting the last content and writing the new one. So input.txt was having ef in that (finally).

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

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

And now you will get what you expected from this. Now input.txt will have the following unlike with your case.

ab
cd
ef

Note: But if you will will run first.py 2nd time, it will continue to add as a+ creates file if file does not exist otherwise it appends. For better working of this code use os.path module's exists() function.

And if you want to call the code available in first.py then wrap it inside the function. Then import that function in second.py and call.

For example

First make sure first.py and second.py are in the same directory.

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')

Open terminal, navigate to this directory, run python second.py.

https://www.learnpython.org/en/Module... | https://www.digitalocean.com... | https://www.programiz.com/pytho... would be the helpers for you if you want to read and try how to create module/package in Python.

Update: The above has a problem as you have mentioned in comment, in each each run, it will append the content. Let's fix it with a little change to first.py as follows.

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)

That is it (update in comment if you are stuck).

10 Comments

Thanks a lot. one more question how to call that input.txt file for second.py program directly instead of calling like this --> input_file = open('input.txt','r')
how to import that function in second.py and call please give me a solution.
I have updated my answer, please check and I am sorry for answering your query late.
thanks a lot. when i run the code for first time i got the correct output but when i run again it print the answer twice then again i run the code it print thrice ..i dont know why pls suggest me
Okay got it. You mean in each run, you want only the same o/p. Do one thing, Just check if file exists or not. If it exists, delete it and recreate it. So in one run, only the contents of the list will be written to the file. In 2nd session, program will detect the existence and delete it. Again it will create the same as before. Just check I have updated my answer. I have kept my old too so that you could understand the difference properly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.