Jump to content

Why doesn't this work? Python3

Poet129
Go to solution Solved by Poet129,
2 hours ago, Poet129 said:

Okay now what?

  Reveal hidden contents


file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
file3 = open("/home/akerr/Documents/Temp.txt", "w")
file3.write("")
file3.close()
count = 0
for line in file1.readlines(  ): count += 1
s = str(count)
file1.close()
j = 1
l1 = 1
def ConvertBinaryToString(l1):
    global line1
    file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
    file2 = open("/home/akerr/Programs/AC.16", "r")
    l2 = 1
    for line1 in file1:
            for line2 in file2:
                if (line2 == line1):
                    temp = open("/home/akerr/Documents/Temp.txt", "a")
                    temp.write(str(l2) + "\n")
                    print(l2, line2)
                    temp.close()
                l2 += 1
while j in range (1, int(s)):
    ConvertBinaryToString(l1)
    j += 1
    l1 += 1
if (len(line1) < 16):
    temp = open("/home/akerr/Documents/Temp.txt", "a")
    print(line1)
    tempdata = str(line1)
    temp.write(tempdata)
    temp.close()

 

Output is

  Reveal hidden contents

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

0001010

Output should be

  Reveal hidden contents

43211 1010100011001010

59113 1110011011101000

16665 0100000010001100

53977 1101001011011000

51805 1100101001011100

0001010

 

The completed code

Spoiler

file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
file3 = open("/home/akerr/Documents/Temp.txt", "w")
file3.write("")
file3.close()
count = 0
for line in file1.readlines(  ): count += 1
s = str(count)
file1.close()
j = 1
l1 = 1
def ConvertBinaryToString(l1, file1):
    global line1
    file2 = open("/home/akerr/Programs/AC.16", "r")
    l2 = 1
    for line2 in file2:
        if (line2 == line1):
            temp = open("/home/akerr/Documents/Temp.txt", "a")
            temp.write(str(l2) + "\n")
            print(l2, line2)
            temp.close()
        l2 += 1
while j in range (1, int(2)):
    file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
    for line1 in file1:
        ConvertBinaryToString(l1, file1)
    j += 1
    l1 += 1
if (len(line1) < 16):
    temp = open("/home/akerr/Documents/Temp.txt", "a")
    print(line1)
    tempdata = str(line1)
    temp.write(tempdata)
    temp.close()

 

 

Spoiler

file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
file2 = open("/home/akerr/Programs/AC.16", "r")
file3 = open("/home/akerr/Documents/Temp.txt", "w")
file3.write("")
file3.close()
l1 = 1
l2 = 1
l3 = 1
for line1 in file1:
    if (int(l3) == 1):
        for line2 in file2:
            if (line2 == line1):
                temp = open("/home/akerr/Documents/Temp.txt", "a")
                temp.write(str(l2))
                print(l2, line2)
                temp.close()
                l3 = 0
            if not (line2 == line1):
                l3 = 1
            l2 += 1
        l1 += 1
    l3 = 1

 

I'm trying to write every line of a file where LineOfFile2 = LineOfFile1 then write it to temp file but it is only doing one so far but I have more matching data.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Slottr said:

Get rid of l3

It's redundant

Okay now what?

Spoiler

file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
file3 = open("/home/akerr/Documents/Temp.txt", "w")
file3.write("")
file3.close()
count = 0
for line in file1.readlines(  ): count += 1
s = str(count)
file1.close()
j = 1
l1 = 1
def ConvertBinaryToString(l1):
    global line1
    file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
    file2 = open("/home/akerr/Programs/AC.16", "r")
    l2 = 1
    for line1 in file1:
            for line2 in file2:
                if (line2 == line1):
                    temp = open("/home/akerr/Documents/Temp.txt", "a")
                    temp.write(str(l2) + "\n")
                    print(l2, line2)
                    temp.close()
                l2 += 1
while j in range (1, int(s)):
    ConvertBinaryToString(l1)
    j += 1
    l1 += 1
if (len(line1) < 16):
    temp = open("/home/akerr/Documents/Temp.txt", "a")
    print(line1)
    tempdata = str(line1)
    temp.write(tempdata)
    temp.close()

 

Output is

Spoiler

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

0001010

Output should be

Spoiler

43211 1010100011001010

59113 1110011011101000

16665 0100000010001100

53977 1101001011011000

51805 1100101001011100

0001010

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Poet129 said:

Okay now what?

  Reveal hidden contents


file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
file3 = open("/home/akerr/Documents/Temp.txt", "w")
file3.write("")
file3.close()
count = 0
for line in file1.readlines(  ): count += 1
s = str(count)
file1.close()
j = 1
l1 = 1
def ConvertBinaryToString(l1):
    global line1
    file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
    file2 = open("/home/akerr/Programs/AC.16", "r")
    l2 = 1
    for line1 in file1:
            for line2 in file2:
                if (line2 == line1):
                    temp = open("/home/akerr/Documents/Temp.txt", "a")
                    temp.write(str(l2) + "\n")
                    print(l2, line2)
                    temp.close()
                l2 += 1
while j in range (1, int(s)):
    ConvertBinaryToString(l1)
    j += 1
    l1 += 1
if (len(line1) < 16):
    temp = open("/home/akerr/Documents/Temp.txt", "a")
    print(line1)
    tempdata = str(line1)
    temp.write(tempdata)
    temp.close()

 

Output is

  Reveal hidden contents

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

43211 1010100011001010

0001010

Output should be

  Reveal hidden contents

43211 1010100011001010

59113 1110011011101000

16665 0100000010001100

53977 1101001011011000

51805 1100101001011100

0001010

 

The completed code

Spoiler

file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
file3 = open("/home/akerr/Documents/Temp.txt", "w")
file3.write("")
file3.close()
count = 0
for line in file1.readlines(  ): count += 1
s = str(count)
file1.close()
j = 1
l1 = 1
def ConvertBinaryToString(l1, file1):
    global line1
    file2 = open("/home/akerr/Programs/AC.16", "r")
    l2 = 1
    for line2 in file2:
        if (line2 == line1):
            temp = open("/home/akerr/Documents/Temp.txt", "a")
            temp.write(str(l2) + "\n")
            print(l2, line2)
            temp.close()
        l2 += 1
while j in range (1, int(2)):
    file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
    for line1 in file1:
        ConvertBinaryToString(l1, file1)
    j += 1
    l1 += 1
if (len(line1) < 16):
    temp = open("/home/akerr/Documents/Temp.txt", "a")
    print(line1)
    tempdata = str(line1)
    temp.write(tempdata)
    temp.close()

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Poet129 said:

I'm trying to write every line of a file where LineOfFile2 = LineOfFile1 then write it to temp file but it is only doing one so far but I have more matching data

Once you've iterated through file2, the pointer in that file will be at the end of the file and you need to seek to the beginning again:

file1 = open("/home/akerr/Downloads/Test.txt.16", "r")
file2 = open("/home/akerr/Programs/AC.16", "r")
file3 = open("/home/akerr/Documents/Temp.txt", "w")
l1 = 1
l2 = 1
for line1 in file1:
    # Reset the position in file2 to the beginning
    file2.seek(0)
    for line2 in file2:
        if (line2 == line1):
            file3.write(str(l2))
            print(l2, line2)
        l2 += 1
    l1 += 1
file3.close()

Tbh, your code is a mess and probably isn't doing what you intended anyways, but that seek() there fixes the issue at hand.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×