The Artima Developer Community
Sponsored Link

Python Answers Forum
is exec() what I want?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
David Huckaby

Posts: 1
Nickname: huckda
Registered: Aug, 2004

is exec() what I want? Posted: Aug 12, 2004 5:53 PM
Reply to this message Reply
Advertisement
here's a little .py script I wrote to create usernames for students from a textfile...
what I'd like it to do also as another menu option...
is to 'adduser <newusername>' in linux...using K12LTSP 4.1.0

any pointers would be lovely!

import string
 
def print_menu():
   print '1. Press 1 to (i)mport the namelist.txt file'
   print '2. Press 2 to (g)enerate usernames'
   print '3. Press 3 to (p)rint new usernames.txt file'
   print '4. Press 4 to E(x)it the Program'
numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 4:
   menu_choice = input("Type in a number (1-4):")
   if menu_choice == 1:
      #Open the file "namelist.txt"
      in_file = open("c:\\namelist.txt","r")
      #variable "textfile" gets filled with info from namelist.txt
      textfile = in_file.readlines()
      #Close the file "namelist.txt"
      in_file.close()
      
   elif menu_choice == 2:     
      #The Loopy Loopy
      namenum = 0
      out_file = open("c:\\my documents\pythonscripts\usernames.txt","w")
      print namenum
      while namenum < len(textfile):
         #Manipulate the namelist into pieces we can handle
         mystring = textfile[namenum]
         newlist = mystring.split()
         fname = newlist[0]
         lname = newlist[1]
         fname = fname[:2]
         lname = lname[:4]
         #Concatenate and Write to File
         out_file.write(lname+fname+"\n")
         print lname+fname+" has been written to file." 
         namenum = namenum+1
         print namenum
      out_file.close()
      namenum = 0
   elif menu_choice == 3:
      #Gimme some output baby!
      in_file = open("c:\\my documents\pythonscripts\usernames.txt","r")
      funkytext = in_file.readlines()
      in_file.close()
      print funkytext
   elif menu_choice == 4:
      pass
   else:
      print_menu()
print "Goodbye!"


Thanks,
--Huck

Topic: dir(__builtin__)  and dir("__builtin__") Previous Topic   Next Topic Topic: Playing with decorators

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use