From 7923dcc334cb08853850a3165ffd33caedd06935 Mon Sep 17 00:00:00 2001 From: Kom Senapati Date: Sun, 8 Oct 2023 16:28:50 +0000 Subject: [PATCH 1/2] .dat file removed logically --- CS_Source Code.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CS_Source Code.py b/CS_Source Code.py index da0891c..d53bdc3 100644 --- a/CS_Source Code.py +++ b/CS_Source Code.py @@ -180,6 +180,13 @@ def delete_roll(): os.rename("temp.dat", "stud.dat") input("Press any key to continue....") +if not os.path.isfile("stud.dat"): + try: + file = open("stud.dat","wb") + file.close() + print("New file 'stud.dat' created.") + except Exception as e: + print(f"Error: {e}") while True: os.system("cls") From 592b1e4b4d3eb922d2d17529f7260527f1dd8565 Mon Sep 17 00:00:00 2001 From: Kom Senapati Date: Sun, 8 Oct 2023 16:52:06 +0000 Subject: [PATCH 2/2] Made the other changed --- CS_Source Code.py | 29 +++++++++++++++++++++++------ README.md | 4 ++++ create_dat.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 create_dat.py diff --git a/CS_Source Code.py b/CS_Source Code.py index d53bdc3..ab333ba 100644 --- a/CS_Source Code.py +++ b/CS_Source Code.py @@ -181,12 +181,29 @@ def delete_roll(): input("Press any key to continue....") if not os.path.isfile("stud.dat"): - try: - file = open("stud.dat","wb") - file.close() - print("New file 'stud.dat' created.") - except Exception as e: - print(f"Error: {e}") + print("'stud.dat' file doesn't exist.") + print("1 -> Generate a new empty file") + print("2 -> Generate a file with fake data") + + print(40 * "=") + choice = input("Enter your choice: ") + print(40 * "=") + + if choice == "1": + try: + file = open("stud.dat", "wb") + file.close() + print("New empty file 'stud.dat' created.") + except Exception as e: + print(f"Error: {e}") + elif choice == "2": + print("Generating fake data...") + os.system("python create_dat.py") + print("Fake data generated.") + else: + print("Invalid choice!") + time.sleep(1) + while True: os.system("cls") diff --git a/README.md b/README.md index 8fcf3d7..008bdb8 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,10 @@ For every school, an important task for the administration department is to mana - [MySQL](https://dev.mysql.com/downloads/mysql/) - [MySQL Python Connector](https://dev.mysql.com/downloads/connector/python/) +- fake + ```bash + pip install fake + ``` ##### 2. Install MySQL in Python using below command diff --git a/create_dat.py b/create_dat.py new file mode 100644 index 0000000..531fde5 --- /dev/null +++ b/create_dat.py @@ -0,0 +1,37 @@ +from fake import Faker +import random +import pickle + +class Student: + def __init__(self, roll, name, percentage): + self.roll = roll + self.name = name + self.percentage = percentage + +def generate_fake_records(n): + fake = Faker() + records = [] + + for _ in range(n): + roll = random.randint(1000, 9999) + name = fake.name() + percentage = round(random.uniform(50, 100), 2) + + student = Student(roll, name, percentage) + records.append(student) + + return records + +def save_to_dat(records): + try: + with open("fake_stud.dat", "wb") as file: + for record in records: + pickle.dump(record, file) + print(f"{len(records)} fake records saved to fake_stud.dat") + except Exception as e: + print(f"Error: {e}") + +if __name__ == "__main__": + n = int(input("Enter the number of fake records to generate: ")) + fake_records = generate_fake_records(n) + save_to_dat(fake_records)