Forum Archive

Writing to file?

Dann239

So I had this working perfectly with just strings and a list. Switched to implementing writing to a file (tiny bit more permanent). All that's missing from this is the implementation of the string and complete implementation of the file writing with property settings AND the global value to pass my "inp()" around.

My question is how to implement file writing and reading. I understand the general idea as I've seen a ton of examples. I however, can not get it to work. I don't even know how to prove the txt exists or not through python. So I'm calling on the amazing pythonista community to help a noob!!


import console
import os

def inp():
open('list.txt', 'r')
print list(list)
print "-" *11
for line in list:
print line
s = raw_input().title()
if s == "Q":
start()
return

def view():
console.clear()
print("Viewing Data")
inp()
return

def add():
console.clear()
print("Adding Data")
inp()
open('list.txt', 'a').write(s.title())
add()

def edit():
console.clear()
print(" Remove?")
inp()
if s in list:
list.remove(s)
edit()
else:
edit()

def start():
console.clear()
choice = ["[A]dd to list" , "[V]iew List" , "[E]dit List"]
print("List App")
print('\n'.join(choice))
s = raw_input().title()
if s == "ValueError":
start()
elif s == "A":
add()
elif s == "V":
view()
elif s == "E":
edit()
else:
start()

start()

ccc

One possible implementation: https://gist.github.com/cclauss/6656495

ccc

Updated to refactor and add writeListToFile().

Dann239

Wow!! You don't know how much I appreciate that! I'm going to be studying this for a while. THANK YOU THANK YOU!!