Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing on a File
#1
Hello everybody ,

My code is

for k in [1,2,3]:
    for l in [4,5,6]:
        print("k=" ,k, "l=",l)
As a result of this small code I get :
k= 1 l= 4
k= 1 l= 5
k= 1 l= 6
k= 2 l= 4
k= 2 l= 5
k= 2 l= 6
k= 3 l= 4
k= 3 l= 5
k= 3 l= 6

How can I write each of these combinations on a different file? So I need to have 9 files which contains one of these combinations ? Thank you so much ın advance

Regards
Reply
#2
Look at our tutorial on working with files
https://python-forum.io/Thread-Basic-Files
Reply
#3
Hello ,

I checked the link and I trıed to write the values on a file. But I think I have a problem with for loop because with this code
for k in [1,2,3]:
for l in [4,5,6]:
fw=open("trial.txt","w")
fw.write ("k="+str(k)+";\n")
fw.write("l=" + str(l) + ";\n")
fw.close()


I am getting just :
k=3;
l=6;

but I want to get all the double combinations . Any idea, where is my fault ??? Sad
Reply
#4
You need to change the file name with every trial in order to get 9 different files. Now with every iteration you overwrite the previous one
Reply
#5
Thank you so much . I solved it
Reply
#6
post your code (in code tags) if you like. there are some improvements that can be implemented
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems writing a large text file in python Vilius 4 1,063 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  writing list to csv file problem jacksfrustration 5 2,409 Jul-04-2024, 08:15 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 3,798 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,722 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 4,513 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 3,835 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 6,836 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 4,747 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 6,028 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 3,045 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020