Re: [Tutor] Delete file before function ends
I'll take a peek at the subprocess docs Steve, and see if I can learn something there... Thanks for the suggestion! I'm using WinXP as well Wayne.. Not too sure why yours works, and mine doesn't.. I'll revisit what I'm doing and also check into Steve's suggestion. Adrian On Mon, Oct 6, 2008 at 2:30 PM, W W <[EMAIL PROTECTED]> wrote: > What OS are you using, Adrian? On WinXP, this worked fine: > > import os > > def files(): > os.startfile('myfile.txt') > os.remove('myfile.txt') > > -Wayne > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Delete file before function ends
Thanks for the input folks. Sadly, Tim's suggestion yields the same results as I was getting previously. My second program very graciously tells me that the file I'm trying to open doesn't exist. Like the code snippet I posted, the timer.sleep(x) line just waits the 'x' seconds until opening 'mytextfile.xyz", instead of opening it, and then waiting 'x' seconds to delete the file. Sorry about naming the path to my file so "poorly"!! I'm a little more careful in my programs! I'm a newbie and I was more concerned about an understandable question! As a newbie, Alan, I was kinda scared you'd say that "threads" were the answer here! (It sounds like someone is going to get sucked into a worm hole or something...) Looks like the next class in my Python education is going to be "Threads 101"... Thanks for all the input, I might even be learning something! Warmest regards, Adrian On Mon, Oct 6, 2008 at 1:31 PM, Alan Gauld <[EMAIL PROTECTED]>wrote: > > "Adrian Greyling" <[EMAIL PROTECTED]> wrote > > that creates my "problem"... What I'd like to do, is create a plain text >> file, use the associated program to open said textfile, (using >> os.startfile) >> and after the associated program has what it needs to open the file and >> then >> of course, has the current focus, I'd like to delete the text file >> > > Thats potentially going to cause the associated program to crash > but assuming you know what you are doing there... > > What happens with the code snippet below, is that it doesn't start >> the second program until the function is finished. >> > > Correct, that's what you asked it to do :-) > > time.sleep() in between the os.startfile() and os.remove(), but it just >> delays opening 'mytextfile.xyz' and actually deletes the file before my >> second program can open it up. >> > > Really? That shouldn't happen! > > path = "c:\MyFolder\mytextfile.xyz" >> > > You probably want to either use forward slashes or put > an r in front of the quotes, otherwise Python will treat > the \ as an escape character... > > #bunch of stuff here to create 'mytextfile.xyz" >> os.startfile(path) >> os.remove(path) >> > > If you want the remove to run in parallel with the startfile > you probably need to use threads to start the application > in one thread and then pause and then delete the file in > the other thread. > > Alan > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Delete file before function ends
Greetings all, Not sure if this is possible, but I'll ask anyway. Below is a code snippet that creates my "problem"... What I'd like to do, is create a plain text file, use the associated program to open said textfile, (using os.startfile) and after the associated program has what it needs to open the file and then of course, has the current focus, I'd like to delete the text file in the background, so to speak. (Please assume that the program doesn't lock 'mytextfile.xyz' when it opens it.) What happens with the code snippet below, is that it doesn't really start the second program until the function is finished. I tried using time.sleep() in between the os.startfile() and os.remove(), but it just delays opening 'mytextfile.xyz' and actually deletes the file before my second program can open it up. Any way around this?? path = "c:\MyFolder\mytextfile.xyz" #bunch of stuff here to create 'mytextfile.xyz" os.startfile(path) os.remove(path) Thanks everyone, Adrian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pysqlite and SQLite
Thanks for the response Chad! I'm using Python 2.5.2, so I guess I'm in the clear then, although I'm still getting an unexplained error message. I've taken my program and used GUI2exe to create a standalone executable. When I run said executable on my own WinXP machine, it works just fine. When I attempt to use the same executable (including the "dist" directory) on a (virtually) identical PC, I get the following error message: pysqlite2.dbapi2.OperationalError: near ",": syntax error I understand the error, but not why I get it on one PC and not another... Adrian On Mon, Sep 29, 2008 at 1:09 PM, Chad Crabtree <[EMAIL PROTECTED]> wrote: > If you are using Python 2.5 or newer then no you will not need to install > SQLite. > > On Mon, Sep 29, 2008 at 1:05 PM, Adrian Greyling < > [EMAIL PROTECTED]> wrote: > >> Probably a really dumb question, but... Do I have to download and install >> SQLite before pysqlite will work properly? >> Thanks, >> Adrian >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] pysqlite and SQLite
Probably a really dumb question, but... Do I have to download and install SQLite before pysqlite will work properly? Thanks, Adrian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Current path (of program)
Is it a faux pas to answer your own question?? I found this after I tweaked my search terms.. >From "Dive Into Python" ( http://diveintopython.org/functional_programming/finding_the_path.html)<http://diveintopython.org/functional_programming/finding_the_path.html> pathname = os.path.dirname(sys.argv[0]) currpath = os.path.abspath(pathname) "Regardless of how you run a script, sys.argv[0] will always contain the name of the script, exactly as it appears on the command line. This may or may not include any path information. os.path.dirname takes a filename as a string and returns the directory path portion. If the given filename does not include any path information, os.path.dirname returns an empty string. os.path.abspath is the key here. It takes a pathname, which can be partial or even blank, and returns a fully qualified pathname." Sorry about that folks... Adrian On Thu, Sep 25, 2008 at 3:25 PM, Adrian Greyling <[EMAIL PROTECTED]>wrote: > I've been using "os.getcwd()" to get my program's "current path". I know > it's actually returning my "current working directory", but it's been > working okay, until today... I used py2exe (and InnoSetup) to create a > standalone executable (for Windows) and then a shortcut icon on the desktop > to "MyProg.exe". Problem is, now "os.getcwd()" returns "C:\Documents and > Settings\ME\Desktop\" as it's current working directory, not the "C:\Program > Files\MyCoolProgram\" that I was expecting. > > I'm trying to use a "relative path reference" so that the user can install > the program to whatever directory he/she wants, but that my program won't > "lose track" of the subdirectories it requires for additional files. > > Anyone run into the same sort of problem? Better yet, anyone know how to > solve this? > > Thanks everyone! > Adrian > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Current path (of program)
I've been using "os.getcwd()" to get my program's "current path". I know it's actually returning my "current working directory", but it's been working okay, until today... I used py2exe (and InnoSetup) to create a standalone executable (for Windows) and then a shortcut icon on the desktop to "MyProg.exe". Problem is, now "os.getcwd()" returns "C:\Documents and Settings\ME\Desktop\" as it's current working directory, not the "C:\Program Files\MyCoolProgram\" that I was expecting. I'm trying to use a "relative path reference" so that the user can install the program to whatever directory he/she wants, but that my program won't "lose track" of the subdirectories it requires for additional files. Anyone run into the same sort of problem? Better yet, anyone know how to solve this? Thanks everyone! Adrian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this a "Class" problem?
I appreciate the feedback! I'll check out the pubsub module and see how that works out and I'll subscribe to the wxPython group too! Thanks again! On Fri, Aug 29, 2008 at 5:31 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > On Fri, Aug 29, 2008 at 12:19 PM, Adrian Greyling > <[EMAIL PROTECTED]> wrote: > > > Here's where I get fuzzy... Let's say I've got a "frame_1" object > > that opens a new "frame_2" object. As you've suggested above, I'll use > "m" > > to create an instance of a frame object. Now frame_2 opens a "dialog_1'" > > which asks for information that is sent back to 'frame_2'. How do I > > reference 'frame_2' in this case? Especially when frame_2 hasn't been > > closed and has just been waiting behind dialog_1 until dialog_1 closes. > > When I try to reference it again as "m = frame_2(self)" from a new > function > > definition, aren't I creating a brand new frame_2 object that has "blank" > > attributes, so to speak? > > Generally the way this works is something like: > - frame 2 creates dialog box > - frame 2 shows dialog box and waits for the dialog box to be dismissed > - frame 2 gets result from dialog box > > There are several examples of this in the wx demo, see > MultiChoiceDialog, SingleChoiceDialog, TextEntryDialog. If you are > writing your own custom dialog, make a method that allows the client > code to retrieve the user data from it. > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this a "Class" problem?
Thanks for the response Jeff, although your answer has spawned another question or two! In your answer, you showed that the attribute " MySecondFrame.text_ctrl_2" doesn't exist and to correct that, you suggested the code below. (What I understand from your response is that I can't reference the original object, but I must create an instance of it. Is that right??) def MainToSecond(self, event): # wxGlade: MyMainFrame. m = MySecondFrame(self) m.Show() m.text_ctrl_2.SetValue("This text was generated from the 'MainFrame' window") Here's where I get fuzzy... Let's say I've got a "frame_1" object that opens a new "frame_2" object. As you've suggested above, I'll use "m" to create an instance of a frame object. Now frame_2 opens a "dialog_1'" which asks for information that is sent back to 'frame_2'. How do I reference 'frame_2' in this case? Especially when frame_2 hasn't been closed and has just been waiting behind dialog_1 until dialog_1 closes. When I try to reference it again as "m = frame_2(self)" from a new function definition, aren't I creating a brand new frame_2 object that has "blank" attributes, so to speak? I'm sure I've made things clear as mud, but hopefully with my blathering, someone will undertand my utter confusion! Thanks everyone! Adrian On Mon, Aug 18, 2008 at 3:29 PM, Jeff Younker <[EMAIL PROTECTED]> wrote: > On Aug 18, 2008, at 9:13 AM, Adrian Greyling wrote: > > def MainToSecond(self, event): # wxGlade: MyMainFrame. > MySecondFrame(self).Show() > MySecondFrame.text_ctrl_2.SetValue("This text was generated from > the 'MainFrame' window") > > > The expression MySecondFrame(self) creates a new object. It > initializes the new object by calling the MySecondFrame's __init__ > method. > > class MySecondFrame(wx.Frame): > def __init__(self, *args, **kwds): > # begin wxGlade: MySecondFrame.__init__ > ... > > self.text_ctrl_2 = wx.TextCtrl(self, -1, "", > style=wx.TE_MULTILINE) > ... > > > > The __init__ method calls sets the variable text_ctrl_2 in the object > m. > > Your function MainToSecond is trying to get the attribute > MySecondFrame.text_ctrl_2. > This attribute does not exist. You want to get the attribute > m.text_ctrl_2. So, the method > should be: > > def MainToSecond(self, event): # wxGlade: MyMainFrame. > m = MySecondFrame(self) > m.Show() > m.text_ctrl_2.SetValue("This text was generated from the > 'MainFrame' window") > > > Also, method and function names should always start with a lower case > letter: always > mainToSecond and never MainToSecond > > -jeff > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Is this a "Class" problem?
Hi folks! I hope I'm in the right place to ask this question. I'm new to Python and have been working through some tutorials and have made it to the GUI creation stage. All I was hoping to do with the code below, was to open a "secondary" window and have some text end up on a text_ctrl, but I get an error message that looks like this: Traceback (most recent call last): File "textbox2TEST.py", line 36, in MainToSecond MySecondFrame.text_ctrl_2.SetValue("This text was generated from the 'MainFrame' window") AttributeError: type object 'MySecondFrame' has no attribute 'text_ctrl_2' I'm using wxGlade and SPE together, so almost all of the code is generated for me. I just don't get why it doesn't work, although I think it has to do with one class referencing another class, and I'm obviously not doing that correctly... Any help is much appreciated! Here's the code that created the error: import wx class MyMainFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MyMainFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE) self.button_1 = wx.Button(self, -1, "Click me to bring up second window and write some text") self.__set_properties() self.__do_layout() self.Bind(wx.EVT_BUTTON, self.MainToSecond, self.button_1) # end wxGlade def __set_properties(self): # begin wxGlade: MyMainFrame.__set_properties self.SetTitle("Main Frame") # end wxGlade def __do_layout(self): # begin wxGlade: MyMainFrame.__do_layout sizer_1 = wx.BoxSizer(wx.VERTICAL) sizer_2 = wx.BoxSizer(wx.VERTICAL) sizer_2.Add(self.text_ctrl_1, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) sizer_2.Add(self.button_1, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0) sizer_1.Add(sizer_2, 1, wx.EXPAND, 0) self.SetSizer(sizer_1) sizer_1.Fit(self) self.Layout() # end wxGlade def MainToSecond(self, event): # wxGlade: MyMainFrame. MySecondFrame(self).Show() MySecondFrame.text_ctrl_2.SetValue("This text was generated from the 'MainFrame' window") # end of class MyMainFrame class MySecondFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MySecondFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.text_ctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE) self.button_2 = wx.Button(self, -1, "Click me to close this frame and send some text back to the MainFrame") self.__set_properties() self.__do_layout() # end wxGlade def __set_properties(self): # begin wxGlade: MySecondFrame.__set_properties self.SetTitle("Frame Number Two") # end wxGlade def __do_layout(self): # begin wxGlade: MySecondFrame.__do_layout sizer_3 = wx.BoxSizer(wx.HORIZONTAL) sizer_4 = wx.BoxSizer(wx.VERTICAL) sizer_4.Add(self.text_ctrl_2, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) sizer_4.Add(self.button_2, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0) sizer_3.Add(sizer_4, 1, wx.EXPAND, 0) self.SetSizer(sizer_3) sizer_3.Fit(self) self.Layout() # end wxGlade # end of class MySecondFrame if __name__ == "__main__": app = wx.PySimpleApp(0) wx.InitAllImageHandlers() frame_1 = MyMainFrame(None, -1, "") app.SetTopWindow(frame_1) frame_1.Show() app.MainLoop() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor