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:

print "\nReading..."
                    myfile = open('myfile.txt', 'r').read()
                    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

Recommended Answers

All 4 Replies

Why do you have such goofy indentations?

sorry didn't notice that when i posted it heres it again with correct indentations:

myfile = open('myfile.txt', 'r').read()
                    print "\n",myfile

Assuming that myfile.txt exists, this should work just fine. Works for me.

myfile = open('myfile.txt', 'r').read()
print "\n", myfile

yeah it was working for me but i must of done something in my code think you could take a look?

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.