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 7003955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:08:10+00:00 2026-05-27T21:08:10+00:00

Python noob using 2.7 on Windows. I’m working on making a hierarchy tree view

  • 0

Python noob using 2.7 on Windows. I’m working on making a hierarchy tree view in HTML programatically. I’ve got a file that is outputted similar to this:

0
  2
    4
      6
        8
        8
0
  2
    4
    4
      6
        8
        8
      6
      6

out.txt looks like this:

0
2
4
6
8
8
0
2
4
4
4
6
8
8
6
6

My code:

x = open('out.txt','r')

for current_line in x:
   prev_line = current_line[:-1]
   print "Current: " + current_line
   print "Previous: " + prev_line
   if prev_line > current_line:
       print "Folder"
   elif prev_line == current_line:
       print "Root"

x.close()

The problem I’m encountering is that each time I try to compare the prev_line to the current_line I do not get the desired output. I can do something like if prev_line == "0": print "Root" but that will not work as it is for only that case. Also, it seems like the if portion is being calculated before the next current is done. The results that I get from my current code includes:

Current: 0

Previous: 0
Current: 2

Previous: 2
Current: 4

Previous: 4
Current: 6

Previous: 6
Current: 8

Previous: 8
Current: 8

Previous: 8
Current: 0

Previous: 0
Current: 2

Previous: 2
Current: 4

Previous: 4
Current: 4

Previous: 4
Current: 4

Previous: 4
Current: 6

Previous: 6
Current: 8

Previous: 8
Current: 8

Previous: 8
Current: 6

Previous: 6
Current: 6
Previous:

What am I doing wrong and how do I fix this? I don’t know if this is a string vs int comparison but if it is I’m gonna feel dumb. I’ve tried it but it raises an error when checking the last “Previous:”.

  • 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-05-27T21:08:11+00:00Added an answer on May 27, 2026 at 9:08 pm

    Set prev_line to the value of current_line at the end of the for loop.

    prev_line = ''
    
    for current_line in x: 
       print "Current: " + current_line
       print "Previous: " + prev_line
    
       if prev_line > current_line:
           print "Folder"
       elif prev_line == current_line:
           print "Root"
    
        prev_line = current_line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python noob trying to learn Pylons. I'm using the QuickWiki tutorial ( http://pylonshq.com/docs/en/1.0/tutorials/quickwiki_tutorial/ )
Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__
A relative noob to Python, I've successfully pulled a text file, out of a
Using python to pick it some pieces so definitely a noob ? here but
I'm a noob but I made my application work beautifully using python manage.py runserver
I am a Python noob. I create a class as follows: class t1: x
I am a noob when it comes to python. I have a python script
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python's convention is that variables are created by first assignment, and trying to read
Python's access to environment variables does not accurately reflect the operating system's view of

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.