#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Newbie here, wondering what's up with this code..


    I've been tinkering with Python as my first computer programming language (unless you count Batch and Bash as fully-fledged languages). I think I've done pretty well overall, keeping it neat and easy to read, but the highlighted piece is what I'm having trouble with. I found the code around the web (googled "python append text") and tried to wrestle with it.

    Thanks in advance!

    Code:
    #Python Application
    #login_user.py
    
    #Pathname Finder
    import sys, os
    pathname = os.path.abspath(pathname)
    
    class start_app:
            def open_menu(self):
                    print "Welcome to the [redacted]!\n"
                    print "---------------\n"
                    print "Please log in!\n"
                    
                    time.sleep(5)
    
                    username = raw_input("> ")
                    
                    f = open(user_info.txt", "r")
                    searchlines = f.readlines()
                    f.close()
                    for i, line in enumerate(searchlines):
                            if username in line: print line
                            else f = open("user_info.txt", "a") as myfile:
                                    myfile.write(username)
                            searchfile.close()
                   
    
    start_object = start_app()
    
    start_object.open_menu(self)
    
    print "Thanks for looking at my script!"
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Code:
                    f = open(user_info.txt", "r")
    Here you have a missing quote: "user_info.txt" lacks the opening one.

    Code:
                            else f = open("user_info.txt", "a") as myfile:
    This is totally wrong. “else” should always have a trailing colon. Also, there shouldn’t be anything following it. However, “f = open(...) as myfile:” is not correct in itself. Maybe “else” should be “with”?
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Thanks!


    Originally Posted by SuperOscar
    Code:
                    f = open(user_info.txt", "r")
    Here you have a missing quote: "user_info.txt" lacks the opening one.

    Code:
                            else f = open("user_info.txt", "a") as myfile:
    This is totally wrong. “else” should always have a trailing colon. Also, there shouldn’t be anything following it. However, “f = open(...) as myfile:” is not correct in itself. Maybe “else” should be “with”?
    Thanks for the feedback! Again, I have very little experience; everything highlighted is code I had just googled to help fit in.

    I'm not sure where I can find a definitive guide on commands etc., though I would really love to learn more.

    Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo