Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 12th, 2005, 11:24 PM   #1
Lockick
Newbie
 
Join Date: Aug 2005
Posts: 7
Rep Power: 0 Lockick is on a distinguished road
Help with a moneymanager program

I'm working on a program to manage money. All it does ask you if you want to add or take money from different accounts. I'm having problems with saving the information to a file. I haven't quite mastered File I/O yet, but I was hoping to do so soon.

Anyway, when I run the program it tells me that the file I'm trying to open doesn't exist. I know for a fact that it exists. I've double and triple checked my path, so I'm not looking in the wrong place.

And how to you create a file in Python. I thought that if you used the open("blahblahblah", "r+") function it would open the file or create it if it wasn't there, aparently I was wrong. So now I'm wondering how to create a file in Python.

Some specifics. I've tried making the file a .txt and a .py file, neither way works. I'm not saving a string into the file but pickling a dictionary into it. That's everything that I can think of at the time, tell me if there's anything else you need to help me out and I'd be happy to give it.

Here's the code. Sorry about the lack of comments, I haven't gotten into the habbit of commenting yet, seeing as this is my first non-trivial project. And don't bother checking of any other part of the code besides the fileopen function, unless of course you want to look over the whole thing .
import pickle

def menu():
	print"To enter money, press 1"
	print"To take out money, press 2"
	print"To view your statistics, press 3"
	print"To exit, press 4"
	choice = raw_input("")
	while choice != 1 or 2 or 3 or 4:
		print"Invalid entry"
		print"Enter either 1 or 2 or 3 or 4"
		choice = raw_input("")
	return choice

def fileopen()
	file = open("C:\moneymanagerinfo", "r+")
	try:
		dict = pickle.load(file)
	except:
		dict = {}
		pickle.dump(dict, file)              
	return file

def main()
	if choice != 4:
		
		if choice == 1:
			for x in range(1, 5):
				print"To enter money into account number %d, press %d" % (x)
			secondchoice = input("")
			amount = input("Amount to enter: ")
			for x in range(1, 5):
				if secondchoice == x:
					try:
						dict["accountx"] = amount + dict["accountx"]
					except:
						dict["accountx"] = amount
						
			try:
				dict["total"] = amount + dict["total"]
			except:
				dict["total"] = amount
				
			try:
				dict["totalentered"] = secondchoice + dict["totalentered"]
			except:
				dict["totalentered"] = secondchoice
			del secondchoice
			del amount
			menu()
		
		if choice == 2:
			for x in range(1, 5):
				print"To take money from account number %d, press %d" % (x)
			secondchoice = input("")
			amount = input("Amount to take out: ")
			for x in range(1, 5):
				if secondchoice == x:
					try:
						dict["accountx"] = dict["accountx"] - amount
					except:
						print"Error, account does not exist"
			
			try:
				dict["total"] = dict["total"] - amount
			except:
				dict["total"] = - amount
						
			try:
				dict["totaltaken"] = dict["totaltaken"] + amount
			except:
				dict["totaltaken"] = amount
			del secondchoice
			del amount
			menu()
		
		if choice == 3:
			print"Total amount entered to date is %d" % (dict["totalentered"])
			print"Total amount taken to date is %d" % (dict["totaltaken"])
			print"Total money in accounts is %d" % (dict["total"])
			for x in range(1, 5):
				print"Total money in acount %d is %d" % (x, dict["accountx"])
			menu()
	
	pickle.dump(dict, file)

fileopen()
menu()
main()
Lockick is offline   Reply With Quote
Old Aug 13th, 2005, 7:27 AM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
The + is superfluous in your example, as the + won't create the file.
You need to open the file in write or append mode ("w" or "a") for the file to be created. So just try: ... except: ... around the open call, and if you get an exception create the file by opening it in write mode and then closing it, then trying your code again.
And don't use "except:" - make you specify what kind of error you're excepting.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC