0

How to import and edit a dictionary imported? Everything working except when i re-open the file. File name: Bank2.txt

file = open ("list.txt", "r") print(file.read()) file.close file=open("list.txt","w") print("name : ") nameswap = input() print("cash : ") cashswap = input() blackdict[nameswap] = cashswap file.write(str(blackdict)) file.close() file=open("list.txt","r") print("New List : ",file.read()) print("BLackDict Test : " , blackdict) file.close() input ("PRESS ENTER TO CONTINUE") menu() You can find the full file at my codes Bank2.txt (main.txt main program)

27th Apr 2019, 9:51 AM
Βαγγέλης Τζουβάρας
Βαγγέλης Τζουβάρας - avatar
3 ответов
+ 3
There are several errors in your code (file main.txt). Function/method calls need parentheses: f.close(), exit(). In your try block, you open a file with a context manager (with open (...) as f). Context managers close the file automatically, so you can't close the file manually in line 30. Outside of the scope of the context manager, python doesn't know what the variable "f" stands for. Also, you're not specifying the file mode (r, w, r+ etc.). I'm not sure if the attempt to open a file can lead to a ValueError, but I don't think so. blackdict = f.read() in line 11 will save the file's content in the variable blackdict as best, but it won't create a dictionary. elif choice !="3" in line 31 doesn't seem to make sense. You're checking if choice is either 1 or 2 or NOT 3. Personally, I wouldn't use the extension .txt for a python file, but that's your choice.
27th Apr 2019, 10:09 AM
Anna
Anna - avatar
0
I see thanks for the advices!! They are really helpfull but my main.txt (based on python3 ) works so its not my current concern .my problem is when am changing the values after closing and opening the bank2.txt can you please review this too? ( when am adding entries and everything works perfectly with empty txt(file) but when i'm adding them and trying to edit the entries i get 'str' object does not support item assignment
27th Apr 2019, 10:31 AM
Βαγγέλης Τζουβάρας
Βαγγέλης Τζουβάρας - avatar
0
Am new so am trying many ways. Trying to learn as much as i can do many ways to see/find/learn suggest me anything :D
27th Apr 2019, 10:33 AM
Βαγγέλης Τζουβάρας
Βαγγέλης Τζουβάρας - avatar