Python Forum
Having issues with open,write,read
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having issues with open,write,read
#12
It's not clear what your purpose is or what you are trying to write. If you need help then we need more details. Tracebacks, original code, updated code, code you've tried... etc. Much more detail than you've provided. But in any case here is something very simple that I wrote in order to write look back on to remember how to write to files properly. This helped me come up with a registration form and many other ideas and is very basic... Hashing, dictionaries, checks for username/password matching using dictionary key/value pairs all plays into registration forms. If you are writing software that writes to a file for another file then you need to write it as a class or "module" that can be imported into that file. Also if you are writing to a file that does not exist yet and your program need to create it the use a try/except/else block to do that. You also don't need to use the 'r' parameter when opening to read, files are by default open to read if nothing is specified.

import json

def get_stored_username():
    filename = 'username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username

def get_new_username():
    username = input("What is your name? ")
    filename = 'username.json'
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
    return username



def greet_user():
    username = get_stored_username()
    if username:
        valid_username = input("Is " + username + " the correct username? (Y/N): ")
        if valid_username.lower() == 'n':
            username = get_new_username()
            print("We'll remember you when you come back, " + username + "!")
        else:
            print("Welcome back, " + username + "!")
    else:
        username = get_new_username()
        print("We'll remember you when you come back, " + username + "!")

greet_user()
Reply


Messages In This Thread
RE: Having issues with open,write,read - by wavic - Mar-10-2017, 09:22 PM
RE: Having issues with open,write,read - by wavic - Mar-10-2017, 09:33 PM
RE: Having issues with open,write,read - by wavic - Mar-10-2017, 09:40 PM
RE: Having issues with open,write,read - by wavic - Mar-10-2017, 09:50 PM
RE: Having issues with open,write,read - by zivoni - Mar-10-2017, 09:58 PM
RE: Having issues with open,write,read - by Low_Ki_ - Apr-10-2017, 01:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python read PDF Statement and write it into excel mg24 1 988 Sep-22-2024, 11:42 AM
Last Post: Pedroski55
  Delete file with read-only permission, but write permission to parent folder cubei 6 25,511 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,881 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 1,455 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  How do I read and write a binary file in Python? blackears 6 25,281 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read text file, modify it then write back Pavel_47 5 4,701 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 17,029 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Read JSON via API and write to SQL database TecInfo 5 4,867 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  Write and read back data Aggie64 6 3,080 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 8,923 Jul-17-2021, 04:19 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

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