File I/O

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 42
Reputation: tondeuse34 is an unknown quantity at this point 
Solved Threads: 2
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Light Poster

File I/O

 
0
  #1
May 17th, 2008
Hey guys, lately i've been having some trouble with file I/O but usually i don't heres an example of reading a file:
  1. print "\nReading..."
  2. myfile = open('myfile.txt', 'r').read()
  3. print "\n",myfile
but is there and easier way to do that because when i try to run the program just stops, it didn't do that a couple hours ago when i first started writing the program, so any suggestions? thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: File I/O

 
0
  #2
May 18th, 2008
Why do you have such goofy indentations?
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: tondeuse34 is an unknown quantity at this point 
Solved Threads: 2
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Light Poster

Re: File I/O

 
0
  #3
May 18th, 2008
sorry didn't notice that when i posted it heres it again with correct indentations:
  1. myfile = open('myfile.txt', 'r').read()
  2. print "\n",myfile
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: File I/O

 
0
  #4
May 18th, 2008
Assuming that myfile.txt exists, this should work just fine. Works for me.
  1. myfile = open('myfile.txt', 'r').read()
  2. print "\n", myfile
Last edited by Ene Uran; May 18th, 2008 at 1:35 pm. Reason: for
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: tondeuse34 is an unknown quantity at this point 
Solved Threads: 2
tondeuse34's Avatar
tondeuse34 tondeuse34 is offline Offline
Light Poster

Re: File I/O

 
0
  #5
May 18th, 2008
yeah it was working for me but i must of done something in my code think you could take a look?
  1. print """
  2. ***********************************************************
  3. *|----------| - - ____ | ______ ____ *
  4. *|----------| - | - | |------ | | | | *
  5. *| | | | | |_____| | | *
  6. ***********************************************************
  7. """
  8. a = 0
  9. x = 0
  10. import time
  11.  
  12. user1 = 'Sam'
  13. user2 = 'Bob'
  14. user3 = 'Tom'
  15. user4 = 'Joe'
  16.  
  17. pass1 = '13'
  18. pass2 = 'bubble'
  19. pass3 = 'we'
  20. pass4 = 'admin'
  21.  
  22. users = ['Sam','Bob','Tom','Joe']
  23. passwords = ['13','bubble','we','admin']
  24.  
  25.  
  26.  
  27. username = raw_input("Username: ")
  28. if username == user1:
  29. password = raw_input("Password: ")
  30. x+=1
  31. if x == 1:
  32. a = 0
  33. if password.lower() == pass1:
  34. print "Welcome %s, You have a level of 1 on the server" % username
  35. print ""
  36. print "You can: "
  37. print "1) Read a file"
  38. print "2) Log Out"
  39.  
  40. choice = raw_input("Choose your option: ")
  41.  
  42. if choice == 1:
  43. print "\nReading..."
  44. myfile = open('myfile.txt', 'r').read()
  45. print "\n",myfile
  46.  
  47.  
  48.  
  49. elif choice == 2:
  50. time.sleep(5)
  51. raw_input("Press any key to continue.....")
  52. if username == user2:
  53. password = raw_input("Password: ")
  54. if password.lower() == pass2:
  55. print "Welcome %s, You have a level of 2 on the server" % username
  56. print ""
  57. print "You can: "
  58. print "1) Read a file"
  59. print "2) Create a file"
  60. print "3) Log Out"
  61.  
  62. choice = raw_input("Choose your option: ")
  63.  
  64. if choice == 1:
  65. print "\nReading..."
  66. myfile = open('myfile.txt', 'r').read()
  67. print "\n",myfile
  68.  
  69. elif choice == 2:
  70. name = raw_input("Desired File Name: ")
  71. myfile = open('myfile.txt', 'w')
  72. myfile.write(name)
  73. myfile = open('myfile.txt', 'r')
  74.  
  75. elif choice == 3:
  76. time.sleep(5)
  77. raw_input("Press any key to continue.....")
  78. if username == user3:
  79. password = raw_input("Password: ")
  80. if password.lower() == pass3:
  81. print "Welcome %s, You have a level of 3 on the server" % username
  82. print ""
  83. print "You can: "
  84. print "1) Read a file"
  85. print "2) create a file"
  86. print "3) Create Directorys"
  87. print "4) Log Out"
  88.  
  89. choice = raw_input("Choose your option: ")
  90.  
  91. if choice == 1:
  92. myfile = open('myfile.txt', 'w')
  93. myfile = open('myfile.txt', 'r').read()
  94. print "\n",myfile
  95.  
  96.  
  97. elif choice == 2:
  98. name = raw_input("Desired File name: ")
  99. myfile = open('myfile.txt', 'w')
  100. myfile.write(name)
  101. myfile.close()
  102.  
  103. elif choice == 3:
  104.  
  105. if choice == 4:
  106. time.sleep(5)
  107. raw_input("Press any key to continue.....")
  108.  
  109. if username == user4:
  110. password = raw_input("Password: ")
  111. if password.lower() == pass4:
  112. print "Welcome %s, You have a level of 4 on the server" % username
  113. print ""
  114. print "You can: "
  115. print "1) Read a file"
  116. print "2) Create a file"
  117. print ""
  118. print "3) Create Directorys"
  119. print "4) Kick out a user"
  120. print ""
  121. print "5) Change Username"
  122. print "6) Change Password"
  123. print ""
  124. print "7) View Users"
  125.  
  126. choice = raw_input("Choose your option: ")
  127.  
  128. if choice == 1:
  129. print "\nReading..."
  130. myfile = open('myfile.txt', 'r').open()
  131. print "\n",myfile
  132.  
  133. elif choice == 2:
  134. name = raw_input("Desired File name: ")
  135. myfile.write(name)
  136. myfile = open('name', 'w')
  137. print "\n",myfile
  138.  
  139. elif choice == 7:
  140. print "Users & Passwords: "
  141. print ""
  142. print users
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC