1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Python help. Why doesn't the first for loop execute properly?

Discussion in 'Software Development' started by cybarite, Jul 26, 2006.

Thread Status:
Not open for further replies.
  1. cybarite

    cybarite Thread Starter

    Joined:
    Mar 28, 2006
    Messages:
    8
    Hi,

    This is a small script that i am trying to get to execute. It should take the hosts in the first for loop and then for that host process the name in the list stored at c:\fldrname.txt file. But the code just executes the first host and stops the second one is not taken. Why?:confused: Any and all help is appreciated?

    Code:
    import string	
    ourfile = r'c:\fldrname.txt'
    hosts = ['a', 'b']
    fldrlistfile = file(ourfile)
    for x in hosts:
        for i in fldrlistfile.readlines():
            fldr = i.split('\n')
    	print x + fldr[0]
    fldrlistfile.close()
    
    -----------------
    Editing this since i got it to work. It seems assigning the fldrlistfile.readlines() to a variable did the trick.

    Code:
    import string	
    ourfile = r'c:\fldrname.txt'
    hosts = ['a', 'b']
    fldrlistfile = file(ourfile)
    z = fldrlistfile.readlines()
    for x in hosts:
        for i in z:
            fldr = i.split('\n')
    	print x + fldr[0]
    fldrlistfile.close()
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/486477

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice