Alejandro Trinchero
Alejandro Trinchero

Reputation: 93

How to create text files with specific names and access to them

I'm creating a project in which I have a log in system (very basic) and then I need to create different txt files with specific names like: T1, T2, T3... And then have access to each txt file and reset the data in them.

This is basically a restaurant software in which a user logs in with name and then proceeds to create a file for certain table number. In that txt file I plan to save the bill that will be a string code and the price attached.

So far I know that if you open() a file that does not exist it will be created.

while True:
    action = input ('''
 - NEW table
    \n - ADD table
    \n - BILL
    \n - ... ''')

    if action.lower() == 'new':
        t = open('T.txt', 'w')


    elif action.lower() == 'add':
        table = input ('Select desired table number')

    elif action.lower() == "exit":
        exit()

On the second elif is where I plan to select the desired file with user input.

Full code:

with open('names.txt', 'r') as r :
    f_n = r.read().splitlines()
print("Welcome to NAME.app")
##############
# USER LOGIN #
##############
while True:
    name = input("""
    \n - Insert name to logg in
    \n - ADD to save new user
    \n - LIST to see saved users
    \n - REMOVE to delete a user
    \n - EXIT to finish
    \n - ...""")

    lname = name.lower()

    if lname == "add":
        n_input = input("Name:")
        with open('names.txt', 'a') as f:
            f.write(n_input + '\n')

    elif lname == "list":
        with open('names.txt') as f:
            print(f.read().splitlines())
            f.close()

    elif name in f_n:
        print("Logged as", name.upper())
        input('Welcome, press enter to continue \n')
        break

    elif lname == 'remove':
        rem = input("Insert user name to remove \n ...")
        with open('names.txt', 'r+') as f:
            l = f.readlines()
            l = [z for z in l if rem not in z]
        with open('names.txt', 'w') as f:
            f.writelines(l)

    elif lname == "exit":
        exit()
####################
# TABLE MANAGEMENT #
####################
while True:
    action = input ('''
 - NEW table
    \n - ADD table
    \n - BILL
    \n - ... ''')

    if action.lower() == 'new':
        t = open('T.txt', 'w')


    elif action.lower() == 'add':
        table = input ('Select desired table number')

    elif action.lower() == "exit":
        exit()

Thank you.

Upvotes: 0

Views: 878

Answers (2)

Alejandro Trinchero
Alejandro Trinchero

Reputation: 93

The solution found thank to SkiBoxing is to create a variable with input of which number the file(table) will be. This followed by another variable that combines the string 'T' and the input number. As result you always get: T(input).txt

if action == 'new' :
        tn = input('Insert table number \n - ...')
        name = 'T' + tn
        t = open(name + '.txt', 'w+')
        print('Done')

Thank you.

Upvotes: 1

SkiBoxing
SkiBoxing

Reputation: 42

I am not sure that will work but if you put a variable other than the text like : ‘ name = “T1.txt” ’ and next ‘ f = open(name, “w+”) ‘. Next (if this code is working) you can make a function to create new name (the variable)

Upvotes: 1

Related Questions