5 Replies - 303 Views - Last Post: 09 July 2013 - 11:43 AM Rate Topic: -----

#1 laffy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-July 13

Problem with write to file code

Posted 09 July 2013 - 06:34 AM

I'm trying to write a list to a file...have tried to place code here...

def GetNames ( ):
    """Get names from the keyboard
            Append to a list
            Return the list
    """
    lstNames = [ ]
    strName = "dummy"
    while len(strName) > 0:
        strName = raw_input("Enter name or press ENTER key to quit: ")
        if len(strName) > 0:  #did the user press enter to quit?
            lstNames.append(strName.title( ))
filNames = open("f:/names/Namestxt", "w")
filNames.writelines(lstNames)
filNames.close()

    return lstNames

##### Main #####

#print GetName( )
lstStudents = GetNames( )
lstStudents.sort()
print lstStudents



can't get it to work

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with write to file code

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Problem with write to file code

Posted 09 July 2013 - 08:10 AM

Keep in mind that Python uses indentation to group statements in to code blocks.
something = true

if something:
    print "This will be printed if something is true"
print "This will always be printed"


def DoStuff():
    print "This belongs to the DoStuff function"
print "This does NOT belong to the DoStuff function"


Get what I mean?

The code on lines 12, 13 and 14 of your snippet have no indentation. They do not belong to the GetNames function. You need to make sure they are indented correctly; that they are actually in the code block they are meant to be executed with.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7883
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: Problem with write to file code

Posted 09 July 2013 - 08:11 AM

Quote

can't get it to work


What does this mean? What does it not do when it doesn't work?
Describing the problem well is the best place to start when you want to solve it.
Was This Post Helpful? 0
  • +
  • -

#4 laffy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-July 13

Re: Problem with write to file code

Posted 09 July 2013 - 08:46 AM

I can't get it to write the txt file
Was This Post Helpful? 0
  • +
  • -

#5 DeviantYoda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-July 13

Re: Problem with write to file code

Posted 09 July 2013 - 10:23 AM

Not sure if you know this, but you're not creating a .txt file, just something named Namestxt
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7883
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: Problem with write to file code

Posted 09 July 2013 - 11:43 AM

Once you've revised your source code as Atli suggests, you next want to think about the structure of GetNames. It would probably make more sense to break that up - you want to get the list of names, and then pass that list of names to a function that writes them to a file.

Doing this will help you isolate your problem: you'll know whether you're having trouble with getting the names, or with writing them to the file. Finding the problem makes it easier to fix it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1