Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 1st, 2012, 12:46 AM
rhenzar rhenzar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 rhenzar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 33 sec
Reputation 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!"

Reply With Quote
  #2  
Old November 1st, 2012, 02:10 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 4 h 55 m 54 sec
Reputation Power: 65
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #3  
Old November 1st, 2012, 01:32 PM
rhenzar rhenzar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 rhenzar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 33 sec
Reputation Power: 0
Thanks!

Quote:
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!

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Newbie here, wondering what's up with this code..

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap