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
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,370 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 998 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,486 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 2,688 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,739 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,412 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 3,782 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,365 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE
  Writing to File Issue Flash_Stang 3 2,514 Jun-05-2020, 05:14 AM
Last Post: Gribouillis
  Help! Formatting and Writing to a File bwdu 2 2,410 Apr-19-2020, 09:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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