Python Forum

Full Version: I can't write my data to the data file , Why?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to write the data into the text file I want to use data for the next code, but I entered the data file does not write anything.How can I solve this problem Can you help me about that problem ? As I mentioned the title , when I entered the data , It didn't write anything on data text

the problem started when I changed this place

def appendline(n):
n.append(line.split(","))

menu = 0
while menu != 7:
try:
    cevap = str(menu)

    menu = int(input("giris yapmak icin 1e \n toplam saati hesaplamak icin 2ye,\n butun calısanların toplam maasını hesaplamak icin 3 e,\n izin günleri için 4 e\n 45 saat cezasını hesaplamak icin 5 e tıklayınız\n bitirmek için7ya"))
except(SyntaxError,NameError):
    print "1 den 7 ye kadar giriniz"
if menu == 1:
    fin = "data.txt"
    fout = open(fin,'a')
    info = ""
    no = 0
    while info != "no":
        info = raw_input("Veri girmek istiyor musunuz? (yes/no)")
        if info != "no" and info == "yes":
            try:
                no = no + 1
                weekdays1 =int(input("09:00-17:00-pazartesi"))
                weekdays1_2=int (input("17:00-09:00-pazartesi"))
                weekdays2 = int(input("09:00-17:00-sali"))
                weekdays2_2=int (input("17:00-09:00-sali"))

                weekdays3 = int(input("09:00-17:00-carsamba"))
                weekdays3_2=int (input("17:00-09:00-carsamba"))
                weekdays4 = int(input("09:00-17:00-persembe"))
                weekdays4_2=int (input("17:00-09:00-persembe"))
                weekdays5 = int(input("09:00-17:00-cuma"))
                weekdays5_2=int (input("17:00-09:00-cuma"))
                weekend6 = int(input("09:00-17:00-cumartesi"))
                weekend6_2=int (input("17:00-09:00-cumartesi"))
                weekend7 = int(input("09:00-17:00-pazar"))
                weekend7_2=int (input("17:00-09:00-pazar"))



                totalweekdayshour = (weekdays1 + weekdays2 + weekdays3 + weekdays4 + weekdays5+ weekdays1_2+weekdays2_2+weekdays3_2+weekdays4_2+weekdays5_2)
                totalweekendhour = (weekend6 + weekend7+weekend6_2+weekend7_2)

                totalhour = totalweekdayshour + totalweekendhour
                # hafta i?i sabah normal maas, hafta i?i ak?am 1.5 kat? haftasonu 2 ,haftasonu aksam 2.5 kat? maas al?n?r.
                n = int(input("saatlik kazanc"))


                m1 = (weekdays1) * n
                m1_2 = (weekdays1_2) * n* 1.5

                m2 = (weekdays2) * n
                m2_2 = (weekdays2_2) * n* 1.5

                m3 = (weekdays3 ) * n
                m3_2 = (weekdays3_2) * n*1.5
                m4 = (weekdays4 ) * n
                m4_2 = (weekdays4_2) * n *1.5
                m5 = (weekdays5 ) * n
                m5_2 = (weekdays5_2) * n* 1.5

                m6 = (weekend6)* 2 * n
                m6_2 = (weekend6_2) *2.5*n
                m7 = (weekend7) (2 n)
                m7_2 = (weekend7_2)* 2.5*n

                totalsalary = m1+m1_2+m2+m2_2+m3+m3_2+m4+m4_2+m5+m5_2+m6+m6_2+m7+m7_2

                if weekdays1 >= 9 :
                    if weekdays1_2 >= 9 :
                       if weekdays2 >= 9 :
                          if weekdays2_2 >= 9 :
                             if weekdays3 >= 9 :
                                if weekdays3_2 >= 9 :
                                    if weekdays3 >= 9 :
                                        if weekdays3_2 >= 9 :
                                            if weekdays4 >= 9 :
                                                if weekdays4_2 >= 9 :
                                                     if weekdays5 >= 9 :
                                                        if weekdays5_2 >= 9 :


                                                             print ("Hatali calisma saati girisi degerlerınız kaydedılmedı")
            except(SyntaxError,NameError,IndexError):
                print("Oops! Enter a number.")

                break
                fout.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s \n" % ( no, weekdays1,weekdays1_2,weekdays2,weekdays2_2,weekdays3,weekdays3_2,weekdays4,weekdays4_2,weekdays5,weekdays5_2,weekend6,weekend6_2,weekend7,weekend7_2,totalweekdayshour, totalweekendhour, totalhour, totalsalary))
                fout.close()
** Moderator Note ** Please use python, not quote tags (i fixed for you this post)
Your indentation is messed up, so it's really difficult (...or impossible) to guess what your code actually looks like. But from what's been shared, nothing is written to the file, because you only try writing to the file if there's an error, but AFTER a break statement, which means nothing would ever be written, regardless of error states.
(Jan-08-2019, 10:26 PM)nilamo Wrote: [ -> ]Your indentation is messed up, so it's really difficult (...or impossible) to guess what your code actually looks like. But from what's been shared, nothing is written to the file, because you only try writing to the file if there's an error, but AFTER a break statement, which means nothing would ever be written, regardless of error states.
When I delete it still doesnt work
Quote:When I delete it still doesnt work
Try to print() in the console instead of writing to file. Once it works with print(), replace print() with fout.write()
(Jan-08-2019, 10:43 PM)Gribouillis Wrote: [ -> ]
Quote:When I delete it still doesnt work
Try to print() in the console instead of writing to file. Once it works with print(), replace print() with fout.write()


all code like that
def sorunvar ():
    print ("?al?sanlar haftada 45 saatten fazla cal?smamal?d?r mesai ?creti odemel?s?n?z.")

def sorunyok ():
    print ("?al?sma saat?nde sorun yok")


# 1 e t?klayarak g?r?s yap?yoruz cal?sanlarla ?lg?l? 2 de ve 3 de de ayr? ayr? seyler hesaplan?yor 6 ya t?klay?nca duruyor.

menu = 0
while menu != 7:
    try:
        cevap = str(menu)

        menu = int(input("giris yapmak icin 1e \n toplam saati hesaplamak icin 2ye,\n butun cal?sanlar?n toplam maas?n? hesaplamak icin 3 e,\n izin g?nleri i?in 4 e\n 45 saat cezas?n? hesaplamak icin 5 e t?klay?n?z\n bitirmek i?in7ya"))
    except(SyntaxError,NameError):
        print "1 den 7 ye kadar giriniz"
    if menu == 1:
        fin = "data.txt"
        fout = open(fin,'a')
        info = ""
        no = 0
        while info != "no":
            info = raw_input("Veri girmek istiyor musunuz? (yes/no)")
            if info != "no" and info == "yes":
                try:
                    no = no + 1
                    weekdays1 = int(input("09:00-17:00-pazartesi"))
                    weekdays1_2= int (input("17:00-09:00-pazartesi"))
                    weekdays2 = int(input("09:00-17:00-sali"))
                    weekdays2_2= int (input("17:00-09:00-sali"))

                    weekdays3 = int(input("09:00-17:00-carsamba"))
                    weekdays3_2= int (input("17:00-09:00-carsamba"))
                    weekdays4 = int(input("09:00-17:00-persembe"))
                    weekdays4_2= int (input("17:00-09:00-persembe"))
                    weekdays5 = int(input("09:00-17:00-cuma"))
                    weekdays5_2= int (input("17:00-09:00-cuma"))
                    weekend6 = int(input("09:00-17:00-cumartesi"))
                    weekend6_2 =int (input("17:00-09:00-cumartesi"))
                    weekend7 = int(input("09:00-17:00-pazar"))
                    weekend7_2 =int (input("17:00-09:00-pazar"))



                    totalweekdayshour = (weekdays1 + weekdays2 + weekdays3 + weekdays4 + weekdays5+ weekdays1_2+weekdays2_2+weekdays3_2+weekdays4_2+weekdays5_2)
                    totalweekendhour = (weekend6 + weekend7 + weekend6_2 + weekend7_2)

                    totalhour = totalweekdayshour + totalweekendhour
                    # hafta i?i sabah normal maas, hafta i?i ak?am 1.5 kat? haftasonu 2 ,haftasonu aksam 2.5 kat? maas al?n?r.
                    n = int(input("saatlik kazanc"))


                    m1 = (weekdays1) * n
                    m1_2 = ((weekdays1_2) * n)* 1.5

                    m2 = (weekdays2) * n
                    m2_2 = ((weekdays1_2) * n)* 1.5

                    m3 = (weekdays3 ) * n
                    m3_2 = ((weekdays1_2) * n)* 1.5
                    m4 = (weekdays4 ) * n
                    m4_2 = ((weekdays1_2) * n)* 1.5
                    m5 = (weekdays5 ) * n
                    m5_2 = ((weekdays1_2) * n)* 1.5

                    m6 = (weekend6)* 2 * n
                    m6_2 = ((weekdays1_2) * n)* 2.5
                    m7 = (weekend7) *(2* n)
                    m7_2 = ((weekdays1_2) * n)* 2.5

                    totalsalary = m1+m1_2+m2+m2_2+m3+m3_2+m4+m4_2+m5+m5_2+m6+m6_2+m7+m7_2

                    if weekdays1 >= 9 :
                        if weekdays1_2 >= 9 :
                           if weekdays2 >= 9 :
                              if weekdays2_2 >= 9 :
                                 if weekdays3 >= 9 :
                                    if weekdays3_2 >= 9 :
                                        if weekdays3 >= 9 :
                                            if weekdays3_2 >= 9 :
                                                if weekdays4 >= 9 :
                                                    if weekdays4_2 >= 9 :
                                                         if weekdays5 >= 9 :
                                                            if weekdays5_2 >= 9 :


                                                                 print ("Hatali calisma saati girisi degerler?n?z kayded?lmed?")
                except(SyntaxError,NameError,IndexError):
                    print("Oops! Enter a number.")

                    break
                    fout.write("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s \n" % (no, weekdays1,weekdays1_2,weekdays2,weekdays2_2,weekdays3,weekdays3_2,weekdays4,weekdays4_2,weekdays5,weekdays5_2,weekend6,weekend6_2,weekend7,weekend7_2,totalweekdayshour,totalweekendhour,totalhour,totalsalary))
                    fout.close()
#toplam saat? veriyor her bir ?al?san i?in
    elif menu ==2:
        print "2. Selection"
        fin = open("data.txt", "r")
        for line in fin:
            mylist = list(line.split(","))
            no=mylist[0]
            totalweekdayshour=mylist[8]
            totalweekendhour=mylist[9]
            totalhour=mylist[10]
            print 'No : ', no
            print 'Total hour : ', totalhour
            print 'Hafta ici cal?sma saatleri: ', totalweekdayshour
            print 'Hafta sonu cal?sma saatleri: ', totalweekendhour
            print ""
        fin.close()
# .. ama yapmas? gereken her bir ?al?san?n maas?n? toplay?p g?der? hesaplamak
    elif menu == 3:
        fin = open("data.txt","r")
        totalexpence= []
        totalsalary=[]
        i = 0
        a1=0
        for line in fin:
            appendline(totalexpence)
            a1 = a1 + float(totalexpence[i][8])
            i = i + 1

        fin.close

        print ("Total expence:", a1)
        print ""
#?alisan?n ka? gun izin yapt?g?n? ve izin g?nlerini yazan bir program
#bu bozuk yapab?l?rsen?z g?zel olur.
    elif menu ==4:
        print "2. Selection"
        fin = open("data.txt", "r")
        for line in fin:
            mylist = list(line.split(","))
            no=mylist[0]
            totalweekdayshour=mylist[8]
            totalweekendhour=mylist[9]
            totalhour=mylist[10]
            print 'No : ', no
            print 'Total hour : ', totalhour
            print 'Hafta ici cal?sma saatleri: ', totalweekdayshour
            print 'Hafta sonu cal?sma saatleri: ', totalweekendhour
            print ""
        fin.close()
#45 saatten fazla cal?sma yasas?.......
    elif menu ==5 :
        bronk=open("data.txt","r")
        i=0
        totalweekdayshour=[]
        totalweekendhour=[]
        kk=0
        for line in bronk:

            appendline(totalweekdayshour)
            appendline(totalweekendhour)
            kk =float(totalweekdayshour[i][5]) + float(totalweekendhour[i][6])
            i=i+1
        if kk >= 45 :
            sorunvar()

        else:
            kk<= 45
            sorunyok()

        bronk.close

    elif menu == 6:
        kronk= open("data.txt","r")
        totalsalary=[]
        no=[]
        i=0
        for line in kronk:

            appendline(totalsalary)
            appendline(no)
            kk=totalsalary[i][8]
            nn=int(no[i][0])
            i=i+1

            print nn,kk