Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Text editor

Name: Anonymous 2010-03-12 13:24

How does /prog/ like my text editor?

def is_txt(filename):
    if filename[-4:] == '.txt': return True
    else: return False
def file_exists(filename):
    try:
        in_file = open(filename, "rt")
        in_file.close
        return True
    except IOError:
        return False
def list_files():
    import os
    for filename in os.listdir(os.getcwd()):
        if is_txt(filename): print filename
def choice_handler(choice):
    if choice == "##EXIT##": return
    elif choice == "##NEWF##": new_file()
    elif choice == "##OPEN##":
        while True:
            filename = raw_input("Enter file name to open\n##LIST## lists all .txt files in directory\n##BACK## returns to previous menu\n")
            if filename == "##LIST##": list_files()
            elif filename == "##EXIT##": return
            elif filename == "##BACK##": main_menu(); return
            elif not is_txt(filename): print "Error: Filename MUST end in .txt\nTry again."
            else:
                if file_exists(filename): file_io(filename); return
                else: print "Error: File does not exist.\nTry again."
    return
def new_file():
    while True:
        filename = raw_input("Enter a new file name\n##LIST## lists all .txt files in directory\n##BACK## returns to previous menu\n")
        if filename == "##LIST##": list_files()
        elif filename == "##EXIT##": return
        elif filename == "##BACK##": main_menu(); return
        elif not is_txt(filename): print "Error: Filename MUST end in .txt\nTry again."
        else:
            if file_exists(filename):
                overwrite = raw_input("File already exists. Overwrite? (Y/N): ")
                if overwrite.lower() == 'y': 
                    out_file = open(filename, "wt")
                    out_file.close()
                    file_io(filename)
                else: new_file()
            else:
                out_file = open(filename, "wt")
                out_file.close()
                file_io(filename)
            return
def file_io(filename):
    run_io = 1
    print "Type ##EXIT## to exit program\n-----------------------------"
    in_file = open(filename, "rt")
    text = in_file.read()
    print text
    in_file.close()
    while run_io == 1:
        in_text = raw_input()
        if in_text == '##EXIT##':
            try:
                out_file.close()
                return
            except UnboundLocalError:
                return
            run_io = 0
        else:
            in_file = open(filename, "rt")
            text = in_file.read()
            in_file.close()
            out_file = open(filename, "wt")
            out_file.write(text + "\n" + in_text)
            out_file.close()
    return
def main_menu():
    run = 1
    while run == 1:
        choice = raw_input("Options:\n##OPEN## to open a file\n##NEWF## to create a new file\n##EXIT## to exit\n")
        if choice=="##LIST##": list_files()
        elif choice=="##BACK##": print "Error: This is the top menu"
        elif choice=="##HELP##": print "Available commands:\n##BACK##\n##EXIT##\n##HELP##\n##LIST##\n##NEWF##\n##OPEN##"
        elif choice=="##OPEN##" or choice=="##NEWF##" or choice=="##EXIT##":
            choice_handler(choice)
            run = 0
        else: print "Invalid choice, please try again."
main_menu()

Name: Anonymous 2010-03-12 13:33

it's no emacs

Name: Anonymous 2010-03-12 13:53

>>2
emacs is no >>1

Name: Anonymous 2010-03-12 14:31

You can't delete \n bro

Name: Anonymous 2010-03-12 15:11

it's no ed

Name: Anonymous 2010-03-12 16:37

it's no pico

Name: Anonymous 2010-03-12 17:04

v2.0, prog:

def is_valid(filename):
    invalid_characters = ['/','\\','?','<','>','*','"','|']
    for character in filename:
        if character in invalid_characters: return False, character
    return True, filename
def is_txt(filename):
    if filename[-4:] == '.txt': return True
    else: return False
def file_exists(filename):
    try:
        in_file = open(filename, "rt")
        in_file.close
        return True
    except IOError: return False
def list_files():
    import os
    for filename in os.listdir(os.getcwd()):
        if is_txt(filename): print filename
def choice_handler(choice):
    if choice == "##EXIT##": return
    elif choice == "##NEWF##": new_file()
    elif choice == "##OPEN##":
        while True:
            filename = raw_input("Enter file name to open\n##LIST## lists all .txt files in directory\n##BACK## returns to previous menu\n")
            if filename == "##LIST##": list_files()
            elif filename == "##EXIT##": return
            elif filename == "##BACK##": main_menu(); return
            elif not is_txt(filename): print "Error: Filename MUST end in .txt\nTry again."
            elif not is_valid(filename)[0]: print "Error:", is_valid(filename)[1], "is not a legal character.\nTry again."
            else:
                if file_exists(filename): file_io(filename); return
                else: print "Error: File does not exist.\nTry again."
    return
def new_file():
    while True:
        filename = raw_input("Enter a new file name\n##LIST## lists all .txt files in directory\n##BACK## returns to previous menu\n")
        if filename == "##LIST##": list_files()
        elif filename == "##EXIT##": return
        elif filename == "##BACK##": main_menu(); return
        elif not is_txt(filename): print "Error: Filename MUST end in .txt\nTry again."
        elif not is_valid(filename)[0]: print "Error:", is_valid(filename)[1], "is not a legal character.\nTry again."
        else:
            if file_exists(filename):
                overwrite = raw_input("File already exists. Overwrite? (Y/N): ")
                if overwrite.lower() == 'y': 
                    out_file = open(filename, "wt")
                    out_file.close()
                    file_io(filename)
                else: new_file()
            else:
                out_file = open(filename, "wt")
                out_file.close()
                file_io(filename)
            return
def file_io(filename):
    run_io = 1
    print "Type ##EXIT## to exit program\n-----------------------------"
    in_file = open(filename, "rt")
    text = in_file.read()
    print text
    in_file.close()
    while run_io == 1:
        in_text = raw_input()
        if in_text == '##EXIT##':
            try:
                out_file.close()
                return
            except UnboundLocalError:
                return
            run_io = 0
        else:
            in_file = open(filename, "rt")
            text = in_file.read()
            in_file.close()
            out_file = open(filename, "wt")
            out_file.write(text + "\n" + in_text)
            out_file.close()
    return
def main_menu():
    run = 1
    while run == 1:
        choice = raw_input("Options:\n##OPEN## to open a file\n##NEWF## to create a new file\n##EXIT## to exit\n")
        if choice=="##LIST##": list_files()
        elif choice=="##BACK##": print "Error: This is the top menu"
        elif choice=="##HELP##": print "Available commands:\n##BACK##\n##EXIT##\n##HELP##\n##LIST##\n##NEWF##\n##OPEN##"
        elif choice=="##OPEN##" or choice=="##NEWF##" or choice=="##EXIT##": choice_handler(choice); run = 0
        else: print "Invalid choice, please try again."
main_menu()

Name: Anonymous 2010-03-12 17:09

no python3 support.

Name: Anonymous 2010-03-12 17:26

Needs a Brainfuck implementation.

Name: Anonymous 2010-03-12 17:27

It's not an editor without ELIZA.

Name: Anonymous 2010-03-12 23:54

It's no edlin.  thank god

Name: Anonymous 2010-03-12 23:55

not web 2.0 compliant

Name: Anonymous 2010-03-13 8:32

| elif
Stopped reading right there

Name: Anonymous 2010-03-13 8:35

main_menu()

Stopped reading right there.

Name: Anonymous 2010-03-13 11:22

[quote]>def[/quote]

Stopped reading right there

Name: Anonymous 2010-03-13 11:38

FIOC would be decent if HAX MY ANUS HAX MY ANUS MY OTHER CAR IS A CDR.

Name: Anonymous 2010-03-13 12:59

>>        in_file.close
this isn't ruby. function calls in python require ()

Name: Anonymous 2010-03-13 19:06

##OPEN##
file.txt
This editor is shit.
##EXIT##

Name: Anonymous 2011-02-04 18:56

<

Name: Anonymous 2011-02-18 13:41

<-- check 'em dubz

Don't change these.
Name: Email:
Entire Thread Thread List