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
#11
(Mar-10-2017, 09:58 PM)zivoni Wrote: You should remove code that does nothing to increase clarity.

I guess that code that do something starts on line 18. You are opening same file twice (not a problem), under same handle (again not a problem, but very confusing) and you are opening both of them in write mode (or append). That could be problem - they are closed automatically, but their content might depend on order of closing. You should open it on line 18 just for reading - you dont need more. And explicitly close your "second" file after line 28. Even better would be to use with for that file.

Your a natural, giving you both a rating. You were both very helpful. Thanks so much :-D
Hope you have a nice day!
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,449 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 620 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  How do I read and write a binary file in Python? blackears 6 6,601 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read text file, modify it then write back Pavel_47 5 1,593 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,977 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Read JSON via API and write to SQL database TecInfo 5 2,206 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  Write and read back data Aggie64 6 1,878 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,830 Jul-17-2021, 04:19 PM
Last Post: bill_z
  Open and read multiple text files and match words kozaizsvemira 3 6,751 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Read and write active Excel file euras 4 3,500 Jun-29-2021, 11:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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