0

Been trying to figure how to output the first and last name of the student into a txt file but to no luck and have been struggling for an hour trying to figure out how anyone know any tips to help out as I've done everything else up to the point where it's supposed to output i just cant figure out how to make it actually do so

import os inventory = dict() student = "data"

firstname = input("enter your first name ")
lastname = input("enter your LAST name ")
studentnum = input("enter your number id ")




class student:
    def  __init__(user, firstname, lastname, studentnum):
        user.firstname = (firstname)
        user.lastname = (lastname)
        user.studentnum = (studentnum)






x = student (firstname, lastname, studentnum)
print(x.firstname)
print(x.lastname)
print(x.studentnum)




def write_name(student, firstname, lastname, studentnum):
    """ write to a file named student
        3 lines
            1: firstname
            2: lastname
            3: studentnum
     """


    if not os.path.exists(student):
        os.mkdir(student)

    file = open(student + "/" + str(student), "w")
    file.write(firstname + "\n")
    file.write(lastname + "\n")
    file.write(str(studentnum))
    file.close()

    def read_name(student):
        """
        From the student, READ the file located at data/student
        return a dictionary with key-value pairs of
            student
            firstname
            lastname
            studentnum
        """
        with open(student + "/" + str(student), "r") as file:
            content = file.readlines()  # ['firstline\n', 'second line\n']
            return {
                "barcode": student,
                "name": content[0].strip(),
                "description": content[1],
                "price": float(content[2])
            }

        def retrieve_all_names():
            files_and_folders_of_data = os.listdir(student)
            for each_file in files_and_folders_of_data:
                with open(student + "/" + each_file, "r") as file:
                    content = file.readlines()
                    inventory[int(each_file)] = {
                        "firstname": content[0].strip(),
                        "lastname": content[1].strip(),
                        "studentnum": float(content[2].strip() 
                          }
  • See [Read and Writing Files in the Python tutorial](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) – Charles Duffy Sep 18 '22 at 20:42

0 Answers0