Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:50:02+00:00 2026-06-09T06:50:02+00:00

I’m trying to read two files, and compare them on Python (2.7.3) They don’t

  • 0

I’m trying to read two files, and compare them on Python (2.7.3)
They don’t have the same size/order, because I’m working with IDs/names and they won’t “match”.

And I don’t want to read them simultaneously, but “file2” thorough and compare with each line of “file1” to then read another line of “file1” and so on

From what I’ve done, it works poorly, with some problems.

For example, look at this piece of the code:

if split_cronus[0] == split_data[0]:

The program executes everything in the ‘if’, and then exits.
If I call the function again, after:

print final_line + "\n"

It will work for 62 times and then show this error:

  Traceback (most recent call last):
   File "C:\Users\Matheus\Desktop\DBWolfmizator\DBWolfmizator\DBWolfmizator.py", line 40,     in <module>
   File "C:\Users\Matheus\Desktop\DBWolfmizator\DBWolfmizator\DBWolfmizator.py", line 28,     in translate_itemdb
translate_itemdb()

The “line 28” error is shown everytime the program loops.

And then:

   File "C:\Users\Matheus\Desktop\DBWolfmizator\DBWolfmizator\DBWolfmizator.py", line 15, in translate_itemdb
for line2 in data:
   ValueError: I/O operation on closed file

Which means, with the ‘if’ there, I can get only one single match, like if the file had only one line; but with recursion, I can make it work a few more times before the second file ends.

If you didn’t understand:
I have to read two files.
“file1” and “file2”
In theory, it’s reading like this:

file1_line1 -> file2_line1
file1_line1 -> file2_line2
file1_line1 -> file2_line3
...
file1_line2 -> file2_line1
file1_line2 -> file2_line1
...

But when I got a match, the program exits from the loop.
How do I do that?
In PHP it works great, I was trying to make something like a “port”.

Python code:

cronus = open("item_db.txt", "r+")
data = open("idnum2itemdisplaynametable.txt", 'r')
new_item = open("item_db_new.txt", 'w')
def translate_itemdb():
    try:
        try:
            for line in cronus:
                if line.startswith("//") or len(line) < 3:
                    new_item.write(line)
                    continue

                split_cronus = str.split(line, ",")
                del split_cronus[len(split_cronus) - 1]

                for line2 in data:
                    if line2.startswith("//") or len(line2) < 3:
                        continue

                    split_data = str.split(line2, "#")
                    del split_data[len(split_data) - 1]

                    if split_cronus[0] == split_data[0]:
                        split_cronus[1] = str.replace(split_data[1], " ", "_")
                        split_cronus[2] = str.replace(split_data[1], "_", " ")
                        final_line = ','.join(split_cronus)
                        new_item.write(final_line + "\n")
                        print final_line + "\n"

        finally:
            cronus.close()
            data.close()
            new_item.close()

    except IOError:
        raise

    return

translate_itemdb()

PHP code:

<?php
set_time_limit(0);

$Cronus = file('item_db.txt');
$Data = file('idnum2itemdisplaynametable.txt');

for( $i = 0; $Cronus[$i]; $i++ ) {
    if( $Cronus[$i][0] == '/' || strlen($Cronus[$i]) < 3 ) {
        echo $Cronus[$i]."<br />";
        continue;
    }

    $ExplodeCronus = explode( ',', $Cronus[$i] );

    for( $j = 0; @$Data[$j]; $j++ ) {
        if( $Data[$j][0] == '/' || strlen($Data[$j]) < 3 )
            continue;

        $ExplodeData = explode( '#', $Data[$j] );

        if( $ExplodeData[0] == $ExplodeCronus[0] ) {
            $ExplodeCronus[1] = str_replace( " ", "_", $ExplodeData[1] );
            $ExplodeCronus[2] = str_replace( "_", " ", $ExplodeData[1] );
            $Linha = implode( ',', $ExplodeCronus );
                echo $Linha."<br />";
        }
        }
    }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:50:03+00:00Added an answer on June 9, 2026 at 6:50 am

    One way to fix, is move this line:

       data = open("idnum2itemdisplaynametable.txt", 'r')
    

    To just before this line:

       for line2 in data:
    

    That way you re-open the data file as needed. (Adjust your exception handling as needed to close the data file. Consider using with.)

    Secondly, to get a closer port to the PHP code you posted, you could use readlines. You are reading the file several times anyway, after all. Just read them all at once to start with and then do your processing.

    So, leaving the data opening code where it is, you would do:

       data_f = open("idnum2itemdisplaynametable.txt", 'r')
       data = data_f.readlines()
    

    Then your for line2 in data loop is just revisiting an array of lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have two tables with like below codes: Table: Accounts id | username |
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.