Bennet
Bennet

Reputation: 5

Can't Read File (Python)

I have an issue. I am trying to create a file, ask for 2 inputs, convert that into a dictionary, add it into the file, then display it. Here is my code:

addressBook = open("address.txt","w+")
addresses = {}
newName = input("Enter contact's name: ")
newAddress = input("Enter contact's address: ")
addresses[newName] = newAddress
addressBook.write(str(addresses))
print(addressBook.read())
print(addresses)
addressBook.close()

but for some reason, printing only prints the dictionary 'addresses' and not the file's contents (which should also be 'addresses'). It should return the dictionary twice. Once is printing it directly, and once is printing it using the file. However, it only prints it once. The file doesn't print, only the print(dictionary) itself. Moving addressBook.close() to the front before printing doesn't help either. Thanks!

Upvotes: 0

Views: 68

Answers (3)

Spark Monkay
Spark Monkay

Reputation: 442

It is because the file is opened in write mode and not in read mode. Thank you @kabanus for pointing it out!

My corrected answer is at the bottom after the edit:

This works on Python 3 on my machine as expected, meaning it prints the dictionary twice.

addressBook = open("address.txt","w+")
addresses = {}
newName = input("Enter contact's name: ")
newAddress = input("Enter contact's address: ")
addresses[newName] = newAddress
addressBook.write(str(addresses))
addressBook.close()

with open('address.txt', 'r') as addressBook:
    print(addressBook.read())

print(addresses)

If you want to know more about that, here is the corresponding documentation on open: https://docs.python.org/3/library/functions.html#open

EDIT:

You write some characters to your file, meaning that the file pointer points to the character after your string. When you want to call .read() it returns you the text after the file pointer which is empty. This can be solved by just using .seek(0) to reset the file pointer back to the beginning.

addressBook = open("address.txt","w+")
addresses = {}
newName = input("Enter contact's name: ")
newAddress = input("Enter contact's address: ")
addresses[newName] = newAddress
addressBook.write(str(addresses))
addressBook.seek(0)
print(addressBook.read())
print(addresses)
addressBook.close()

Upvotes: 1

mama
mama

Reputation: 2227

You can let the json library to organize your file for you. And if you use the r+ then you can write and read from the same file.

If you use with open, then the file will close it self when you end the indent, and for comunicating the json to the file, you will use load for reading and dump for writing :)

import json

with open("address.json","r") as addressBook:
    try:
        addresses = json.load(addressBook)
    except:
        addresses = {}
    finally:
        print(addresses)

with open("address.json","r+") as addressBook:
    newName             = input("Enter contact's name: ")
    newAddress          = input("Enter contact's address: ")
    addresses[newName]  = newAddress
    addressBook.write(json.dumps(addresses))
    addresses = json.loads(addressBook.read())
    print(addresses)

Hope it helps :) Good luck!

Upvotes: 0

Benyamina Hamza
Benyamina Hamza

Reputation: 16

Try using the following instead of str(addresses):

addressBook.write(addresses.__str__())

Or you can go even fancier and use the json module:

import json
...
addressBook.write(json.dumps(addresses))

Upvotes: 0

Related Questions