Reputation: 695
`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
Upvotes: 2
Views: 643
Reputation: 8730
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. Soinput.txt
was havingef
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 asa+
creates file if file does not exist otherwise it appends. For better working of this code use os.path module'sexists()
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).
Upvotes: 0
Reputation: 107094
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)
Upvotes: 2