Python Forum
unexpected text file output <_io.TextIOWrapper… encoding='cp1252'>
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unexpected text file output <_io.TextIOWrapper… encoding='cp1252'>
#1
When I run the following code I get the following output instead of the file's text:
List of children's names in the whatever group in alphabetical order: <_io.TextIOWrapper name='whatever.txt' mode='a' encoding='cp1252'>
from easygui import*

I run the script and using the buttonboxes I register children and create groups. I then add children to the groups using choiceboxes. The problem then arises when I use the 'List of children in each group' option and I don't know what's causing it nor how to fix it. Any help will be very much appreciated.

I'm using Pycharm on a Win7os.

from easygui import*


def register_child():
    enter1 = enterbox(msg="Enter the child's first name.", title=' ', default='',
                      strip=True, image=None, root=None)
    if enter1 is None:
        main()
        return
    else:
        while enter1 == "" or enter1.isalpha() == False:
            enter1 = enterbox(msg="Invalid entry. Please enter the child's first name using letters only.",
                              title=' ', default='', strip=True, image=None, root=None)

    enter2 = enterbox(msg="Enter the child's last name.", title=' ', default='',
                      strip=True, image=None, root=None)  # ask user to enter child's first name

    if enter2 is None:
        main()
        return
    else:
        while enter2 == "" or enter2.isalpha() == False:
            enter2 = enterbox(msg="Invalid entry. Please enter the child's last name using letters only.",
                              title=' ', default='', strip=True, image=None,
                              root=None)

    child_name = (enter1 + " " + enter2).title()
    file = open("Children.txt", 'a')
    file.write(child_name + "\n")
    file.close()

    register_more_child = ccbox("Would you like to register another child?", "Please choose an option.")
    if register_more_child == 1:
        register_child()
        return
    elif register_more_child == 0:
        main()
        return


def list_children():
    file = open("Children.txt", 'r')  # open the Children.txt file
    lineList = file.readlines()
    lineList.sort()  # sort the list alphabetically
    # display the list
    print("List of children's names in alphabetical order:")
    for line in lineList:
        print(line, end="")
    file.close()
    main()


def list_groups():
    file = open("Groups.txt", 'r')  # open the Children.txt file
    lineList = file.readlines()
    lineList.sort()  # sort the list alphabetically
    # display the list
    print("List of groups' names in alphabetical order:")
    for line in lineList:
        print(line, end="")
        file.close()
    main()
    return


def children_in_group():
    file = open("Groups.txt", 'r')
    lineList = [line.strip() for line in file]
    choice = choicebox("Which group would you like to display?", "Choose a group.",
                       choices=(lineList))
    file.close()

    file1 = open('%s.txt' % choice, 'r')
    line_list = file1.readlines()
    line_list.sort()  # sort the list alphabetically
    # display the list
    print("List of children's names in the " + choice + " group in alphabetical order:")
    for line in line_list:
        print(line, end="")
    file1.close()
    main()
    return


def add_child_to_group():
    file = open("Children.txt", 'r')  # open the Children.txt file
    lineList = file.readlines()
    file.close()
    # lineList.sort()
    choice1 = choicebox('Choose a child to enter into a group.', 'Add child to a group. ', choices=lineList)
    if choice1 is None:
        main()
        return

    else:
        file = open("Groups.txt", 'r')
        lineList = [line.strip() for line in file]

        choice2 = choicebox("Which group would you like to add the children to?", "Choose a group.",
                            choices=lineList)
        file.close()

        file1 = open('%s.txt' % choice2, 'a')
        file1.write(choice2 + "\n")
        file1.close()
        add_more_children_to_groups = ccbox("Would you like to add another child to a group?",
                                            "Please choose an option.")
        if add_more_children_to_groups == 1:
            add_child_to_group()
            return
        elif add_more_children_to_groups == 0:
            main()
            return


def create_group():
    enter = enterbox(msg="Enter the group's name.", title='Create Group ')
    file = open("Groups.txt", 'a')
    file.write(enter + "\n")
    file.close()

    file = open('%s.txt' % enter, 'w')
    file.close()

    more_groups = ccbox("Would you like to create another group?", "Please choose an option.")
    if more_groups == 1:
        create_group()
        return
    else:
        main()
        return


def children_menu():
    title2 = "Children."
    children_menu = ["1: Register a child.", "2: List of registered children."]
    button = buttonbox("Choose an option", title2, choices=children_menu)
    if button == children_menu[0]:
        register_child()
    if button == children_menu[1]:  # user chooses option 2 from the Children menu:
        list_children()


def group_menu():
    title3 = "Groups."
    groups_menu = ["1: Create a group.", "2: List of groups.", "3: Add a child to a group.",
                    "4: List of children in each group."]
    button = buttonbox("Choose an option", title3, choices=groups_menu)
    if button == groups_menu[0]:
        create_group()

    if button == groups_menu[1]:
        list_groups()

    if button == groups_menu[2]:
        add_child_to_group()

    if button == groups_menu[3]:
        children_in_group()


def main():
    title1 = "Main menu."
    main_menu = ["1: Children", "2: Groups", "3: Educators", "4: Tags", "5: Cancel"]
    button = buttonbox("Choose an option", title1, choices=main_menu)

    if button == main_menu[0]:
        children_menu()

    elif button == main_menu[1]:
        group_menu()

    elif button == main_menu[2]:
        title4 = "Educators."
        educator_menu = ["1: Register an educator.", "2: List of registered educators."]
        button = buttonbox("Choose an option", title4, choices=educator_menu)

    elif button == main_menu[3]:
        button = "4"
        title4 = "Tags."
        tags_menu = ["1: Create tags", "2: List of tags."]
        button = buttonbox("Choose an option", title4, choices=tags_menu)

    else:
        print("You cancelled.")
        return
main()
Reply


Messages In This Thread
unexpected text file output <_io.TextIOWrapper… encoding='cp1252'> - by sleepylog - Aug-20-2017, 07:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output Starter 2 534 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 769 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 557 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,278 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,157 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,169 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,749 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,102 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Search text in PDF and output its page number. atomxkai 21 9,231 Jan-21-2022, 06:20 AM
Last Post: snippsat
  How can we transcode encoding file uml url format Anldra12 9 3,483 Jul-25-2021, 09:30 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020