| | |
File I/O
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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: 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
Python Syntax (Toggle Plain Text)
print "\nReading..." myfile = open('myfile.txt', 'r').read() print "\n",myfile
sorry didn't notice that when i posted it heres it again with correct indentations:
Python Syntax (Toggle Plain Text)
myfile = open('myfile.txt', 'r').read() print "\n",myfile
Assuming that myfile.txt exists, this should work just fine. Works for me.
python Syntax (Toggle Plain Text)
myfile = open('myfile.txt', 'r').read() print "\n", myfile
Last edited by Ene Uran; May 18th, 2008 at 1:35 pm. Reason: for
drink her pretty
yeah it was working for me but i must of done something in my code think you could take a look?
Python Syntax (Toggle Plain Text)
print """ *********************************************************** *|----------| - - ____ | ______ ____ * *|----------| - | - | |------ | | | | * *| | | | | |_____| | | * *********************************************************** """ a = 0 x = 0 import time user1 = 'Sam' user2 = 'Bob' user3 = 'Tom' user4 = 'Joe' pass1 = '13' pass2 = 'bubble' pass3 = 'we' pass4 = 'admin' users = ['Sam','Bob','Tom','Joe'] passwords = ['13','bubble','we','admin'] username = raw_input("Username: ") if username == user1: password = raw_input("Password: ") x+=1 if x == 1: a = 0 if password.lower() == pass1: print "Welcome %s, You have a level of 1 on the server" % username print "" print "You can: " print "1) Read a file" print "2) Log Out" choice = raw_input("Choose your option: ") if choice == 1: print "\nReading..." myfile = open('myfile.txt', 'r').read() print "\n",myfile elif choice == 2: time.sleep(5) raw_input("Press any key to continue.....") if username == user2: password = raw_input("Password: ") if password.lower() == pass2: print "Welcome %s, You have a level of 2 on the server" % username print "" print "You can: " print "1) Read a file" print "2) Create a file" print "3) Log Out" choice = raw_input("Choose your option: ") if choice == 1: print "\nReading..." myfile = open('myfile.txt', 'r').read() print "\n",myfile elif choice == 2: name = raw_input("Desired File Name: ") myfile = open('myfile.txt', 'w') myfile.write(name) myfile = open('myfile.txt', 'r') elif choice == 3: time.sleep(5) raw_input("Press any key to continue.....") if username == user3: password = raw_input("Password: ") if password.lower() == pass3: print "Welcome %s, You have a level of 3 on the server" % username print "" print "You can: " print "1) Read a file" print "2) create a file" print "3) Create Directorys" print "4) Log Out" choice = raw_input("Choose your option: ") if choice == 1: myfile = open('myfile.txt', 'w') myfile = open('myfile.txt', 'r').read() print "\n",myfile elif choice == 2: name = raw_input("Desired File name: ") myfile = open('myfile.txt', 'w') myfile.write(name) myfile.close() elif choice == 3: if choice == 4: time.sleep(5) raw_input("Press any key to continue.....") if username == user4: password = raw_input("Password: ") if password.lower() == pass4: print "Welcome %s, You have a level of 4 on the server" % username print "" print "You can: " print "1) Read a file" print "2) Create a file" print "" print "3) Create Directorys" print "4) Kick out a user" print "" print "5) Change Username" print "6) Change Password" print "" print "7) View Users" choice = raw_input("Choose your option: ") if choice == 1: print "\nReading..." myfile = open('myfile.txt', 'r').open() print "\n",myfile elif choice == 2: name = raw_input("Desired File name: ") myfile.write(name) myfile = open('name', 'w') print "\n",myfile elif choice == 7: print "Users & Passwords: " print "" print users
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- Importing SQL Script File - Urgent !! (Database Design)
- Syntax for deleting specified file ( in C++) (C++)
- enabling file sharing (Windows NT / 2000 / XP)
- data file help (C)
- 81TB File Server (Networking Hardware Configuration)
Other Threads in the Python Forum
- Previous Thread: Help!!! TclError
- Next Thread: tkFileDialog askopenfilename() Question
| Thread Tools | Search this Thread |
address aliased anydbm app bash beginner bits changecolor cipher class clear conversion coordinates corners cturtle curves definedlines development dictionary dynamic events examples excel external feet file float format ftp function gui handling homework iframe images import info input java keycontrol line linux list lists loan loop matching mouse number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python random rational raw_input recursion recursive scrolledtext searchingfile shebang singleton split string strings table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable whileloop windows word wx.wizard wxpython xlwt






