How to write multiple dictionaries to a file?
Question:
enter image description hereWhen I run the above code only the Games_shop dictionary is getting written to the something.txt not the Fruit_shop. Can u please help me know what change should i make in the code to get the Fruit_shop dictionary written to the file along with a small justification. Thanks in Advance!!
def writing_to_file(shop) -> None:
file_name = "something" + '.txt'
with open('C:/Users/xyz/PycharmProjects/Shops/{0}'.format(file_name), 'w') as f:
for key, value in shop.items():
f.write(key + 'n')
for key1, value1 in value.items():
f.write(f"{key1} : {value1}n")
f.write(f"{'}'}")
Fruit_shop = {
'Fruits {':{
'A':'Apple',
'B':'Ball'
}
}
Games_shop = {
'Games {':{
'C':'Cricket',
'F':'Football'
}
}
writing_to_file(Fruit_shop)
writing_to_file(Games_shop)
Answers:
Every time you open a file with the open
function and the write flag ‘w’ there are two options: A new file will be created if it doesn’t exist, or the existing file will be truncated and its previous contents will be cleaned up for the new writing. It’s not something python-specific, it’s an operating system’s standard.
Your Fruit_shop
dict was actually written to the file, but the second time you call the function writing_to_file
it truncates the file and overwrites it with only the Games_shop
dict.
If you want to append contents to your file use the append flag ‘a’ instead of ‘w’. To further information, look at the man page for the fopen system call for a list of all possible flags and their effects.
Your problem derives from a misunderstanding of the mode
argument of the open
function. I suggest the Reading and Writing Files section of the Python documentation and more specifically, the documentation of the open
function
In short, the "w"
(write) option deletes everything, so you need to use the "a"
(append) option instead.
enter image description hereWhen I run the above code only the Games_shop dictionary is getting written to the something.txt not the Fruit_shop. Can u please help me know what change should i make in the code to get the Fruit_shop dictionary written to the file along with a small justification. Thanks in Advance!!
def writing_to_file(shop) -> None:
file_name = "something" + '.txt'
with open('C:/Users/xyz/PycharmProjects/Shops/{0}'.format(file_name), 'w') as f:
for key, value in shop.items():
f.write(key + 'n')
for key1, value1 in value.items():
f.write(f"{key1} : {value1}n")
f.write(f"{'}'}")
Fruit_shop = {
'Fruits {':{
'A':'Apple',
'B':'Ball'
}
}
Games_shop = {
'Games {':{
'C':'Cricket',
'F':'Football'
}
}
writing_to_file(Fruit_shop)
writing_to_file(Games_shop)
Every time you open a file with the open
function and the write flag ‘w’ there are two options: A new file will be created if it doesn’t exist, or the existing file will be truncated and its previous contents will be cleaned up for the new writing. It’s not something python-specific, it’s an operating system’s standard.
Your Fruit_shop
dict was actually written to the file, but the second time you call the function writing_to_file
it truncates the file and overwrites it with only the Games_shop
dict.
If you want to append contents to your file use the append flag ‘a’ instead of ‘w’. To further information, look at the man page for the fopen system call for a list of all possible flags and their effects.
Your problem derives from a misunderstanding of the mode
argument of the open
function. I suggest the Reading and Writing Files section of the Python documentation and more specifically, the documentation of the open
function
In short, the "w"
(write) option deletes everything, so you need to use the "a"
(append) option instead.