Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi i am new to python an di recently created a phonebook using the dictionary function and then changed into exe using py2exe. I am facing a problem now i enter names in to the phone book and then when i exit the program and return back all the numbers are gone. SO is there any way to save the names and numbers entered into the program? Please give me the code as i am doing this for my class and they would be mad if the numbers disappeared everytime they exited the phonebook! PLEASE HELP!
If you do not yet want to learn relational databases, NoSQL or cloud solutions, you can start by using shelve module.
Well, the main problem is that you do store new values in your database, which is in this case represented with dict, but your don't save it's condition between scripts executions. The time of existing of an object in your script - while the the script is running by interpreter and an object has some links to it. When your restart your program - you're starting to run your script all over again, and it stores in the dict only the elements, which were specified during the script.
The most simple solution, in my opinion, is to use python pickle module. You're gonna save that dict in a file and then load it in the begging of your scrip and save it at the end.
you need to update script code with something like this:
default = {'Sarah': 7736815441,
'John': 7736815442}
def start():
#some code here, before you're trying to access phone numbers in your dict
try:
phonebook = pickle.load(open("data.pb", "r"))
except IOError:
phonebook = default
#your script here
def exit():
#some code here, last chance to modify your dict,
#so changes will appear in next program executions
pickle.dump(phonebook, open("data.pb", "w"))
hope you're familiar with python functions, if no - you can read about then here
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Im writting a script in Python that takes as an input a timestamp. For the first execution, it should be something as the now() function. However, for further executions, the input parameter should be the last execution time of the same script. This is being done to avoid getting duplicates results.
Can anyone give me a clue please?
As far as I know, there is no "last executed" attribute for files. Some operating systems have a "last accessed" attribute, but even if that's updated on execution, it would also be updated any time the file was read, which is probably not what you want.
If you need to be able to store information between runs, you'll need to save that data somewhere. You could write it to a file, save it to a database, or write it to a caching service, such as memcached.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently I am working on a project in which I need to save n number of images (to be used in the program's scope). Since the number of images to be saved is dynamic, it may end up exhausting the whole space which i have for my project.
I wanted to know that can there be something added to my code so that after 100% completion of my code the images get automatically deleted as I do not need them after the code's execution.
How can this be done?
I need to save images as they are passed as an argument to one of my functions inside my code. If you know how can I pass image without saving it to my function then please comment here
might be an idea to delete the files immediately after you've done the code you need to do i.e
import os
# Open image
# Manipulate image
os.remove(path_to_image)
Keep track of all the image files you're creating, then delete them in a finally block to ensure they'll be deleted even if an exception is raised.
import os
temp_images = []
try:
# ...do stuff
# ...create image at path_to_file
temp_images.append(path_to_file) # called multiple times
# ...other stuff
finally:
for image in temp_images:
os.remove(image)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm planning on using Robobasket to organise my folders like this,
Invoices
- Jan/Feb/Mar
- Customer Number 1/Customer Number 2
- Then the dates the invoices actual arrived on
The files are named, Month/Customer Number/Date.
What I need to be able to do is, right click on the month, and have it print all the invoice for that month. Specifically printed out by Customer Number 1, then all the dates in order, say 3rd, 17th, 31st, then Customer Number 2, 12thst 22nd, etc.
I have several thousand invoices a month, so you can see why manually printing, even just by Customer number is not something I can do.
The system is running Windows 7, although any programs which can work with a server would be great too, as it's entirely possible, that'll happen soon.
I have basic script skills in C, Python, and Forthe; But am willing to learn for the sake of this issue.
The important thing is that someone who has very little computer skills can at the end of this set it going. Hence the desire for it to be accessible through the context menu.
Any and all help would be appreciated.
You have to do multiple things to get that working.
You need a Python environment installed on the machine or you have to pack your script as an executable. Use py2exe for this.
To get all the files in a given directory, you could use the glob module from python, if you need to filter. Or you could use the approach from denis.
You have to add a context menu item via the registry or using a context menu editor. Look there for an instruction.
Ideally, you pack everything into an installer to distribute the stuff. Including a batch script that adds the context menu item to the registry.
You can use this command to get all the files in current directory:
import os
files = os.listdir(os.curdir)
Now you have all the files in files variable. In order to print contents of all the files in current directory, you can follow this post
How do I print the content of a .txt file in Python?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to python, been coding in school for about a year now, but I like to code when I get bored. I have made two programs but both are useless as I have to input each value of the variables every time I start it up. Is there anyway I can save the value of a variable externally so when it loads it will open up the file and assign each variable?
You should use the pickle module for that purpose:
l =[1,2,3,4,5]
import pickle
pickle.dump(l,open("mydata","wb"))
and for getting your variable back:
import pickle
l = pickle.load(open("mydata","rb"))
If you have many variables to save, consider embedding them in a dictionary for instance.
Yo can use the shelve module its pretty simple it puts all variables into a dictionary then when your file reopens you can make shelve set the variables back. Here is a good example of using the shelve module.
To save data to a file, you could use
filehandle = open(filename, 'w')
filehandle.write(string)
filehandle.close()
Preferred in Python is
with open(filename, 'w') as filehandle:
filehandle.write(string)
because the file will be closed upon exiting the with block even if the block exits with an error, and without requiring the programmer to remember to close the file.
Load the values back in with filehandle.readline() or readlines().
You can also use the Python libraries json or csv to facilitate moving data into and out of files. If you have no need to inspect or modify the data in the file using another program (e.g. Notepad++ or MS Excel), you might prefer pickle or shelve.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I currently have a working python code in command line. How can I convert this into a GUI program. I know how to design a GUI(make buttons,callback function, create text field, label widget...). My question is how should be the GUI connected to the existing program. should I make a python file called gui.py and import this in the main program.. ..or should it be in the other way...
eg:
n = int(raw_input('enter an integer: '))
def fx(n):
result = ''
for i in xrange(1,11):
result += "{} x {} = {}\n".format(i,n,i*n)
return result
print fx(n)
the above program will print the multiplication table of an integer. How should be the gui program(with a entry box, button widget, text widget were o/p will be printed). should this program call the GUI code or should I include this code (fx() function) in the GUI class.
As the GUI is the user front-end, and because your function already exists, the easiest is to make GUI class to import the function. On event, the GUI would call the function and handle the display to the user.
In fact, it's exactly what you have done with a Command-Line Interface (CLI) in your example code :)
I would say the answer strongly depends on your choice of GUI-framework to use. For a small piece of code like the one you posted you probably may want to rely on "batteries included" tkinter. In this case I agree to the comment of shaktimaan to simply include the tkinter commands in your existing code. But you have many choices like PyQT, PySide, kivy... All these frameworks have possiblities to seperate programlogic from GUI-view-code, but have different ways to achieve this.
So read about these frameworks if you're not satisfied with tkinter and make a choice, then you can ask again how to do this seperation if you're not sure.