[Tutor] Writing to a file problem....
Hi, I am currently working my way through the .pdf Byte of Python tutorial by Swaroop, C H After about 1/2 way I ran into a problem that my own trouble shooting has failed :-(. The script created is for backing up files into a .zip format. It is written by the author in a *nix environment. However, I am currently on a *doz box. I have made certain changes in order for it to play nice with Windows, but I am still stumped. Here is the script: #!c:\python26\python.exe # Filename : backup_ver1.py import os import time #1. The files and directories to be backed up are specified in a list source = ['c:\Users\Marty\Downloads', 'c:\Users\Marty\Contacts'] #2. The backup must be stored in a main backup directory target_dir = 'J:\\backup\\' #3. The files are backed up into a zip file. #4. The name of the zip archive is the current date and time target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip' #5. We use the zip command to put files in a zip archive zip_command = "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' '.join(source)) # Run the backup if os.system(zip_command) == 0: print 'Sucessful backup to', target else: print 'Backup FAILED' The error I get running the script from a command prompt is: C:\Python26>backup_ver1.py zip I/O error: Invalid argument zip error: Could not create output file ('J:/backup/20081205002535.zip') Backup FAILED >From what I can tell, it might be a problem writing the file in Windows. Maybe a rights issue. I don't know. I have tried granting the folder all the write rights I could think of. If I change the target_dir to just 'j:' I then get the zip program appearing to run through all the files compressing them, but I end up with this msg: total bytes=104598424, compressed=102594319 ->2% savings zip warning: new zip file left as 'J:zia03824 zip I/O error: Invalid argument zip error: Could not create output file (was replacing the original zip file) Backup FAILED Any suggestions would be appreciated. Thanks, Marty. _ Send e-mail faster without improving your typing skills. http://windowslive.com/Explore/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_speed_122008 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to a file problem....
marty, i applaud you in your efforts to port this script to the Win32 platform. the task is not as simple as one may expect, due to the differing file pathname nomenclatures that the different operating systems use. because of this, i have a couple of suggestions: 1. highly recommend converting this script to use the built-in ZIP file support offered by Python via the zipfile module: http://docs.python.org/lib/module-zipfile.html 2. instead of filenames like: 'c:\Users\Marty\Downloads', 'c:\Users\Marty\Contacts', 'J:\\backup\\', etc., i would recommend using "raw strings." these will help deal with the backward slashes. in the 3rd example, you correctly use double-backslashes, but you don't in the 1st pair. for all 3, change all the files to single backslashes and place an "r" preceding the opening quote, i.e., r'c:\Users\Marty\Downloads', r'c:\Users\Marty\Contacts', r'J:\backup'. for the last case where you are using a directory/folder name, use os.path.join() instead of manually placing the filepath separator. if you combine these together, you should be able to rework your script so that things work properly without having to use external calls like os.system(). hope these help! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Jython question
Is this the right forum to ask a jython question? I want to get started on using GUI with my python apps and I already study Java so I thought maybe I should combine the two. I did some playing around with it so far and I like what I see. I'll post my code if you guys handle this. Any thoughts? -- Mike Hoy ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to a file problem....
On Fri, Dec 5, 2008 at 3:51 AM, Marty Pitts <[EMAIL PROTECTED]> wrote: > #5. We use the zip command to put files in a zip archive > zip_command = "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' > '.join(source)) It might help to print zip_command, to make sure it is what you expect. You can take the printed command and try it on the command line yourself. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Random equation generator
I am starting out with 7 fixed reference points. From there I want a program that can randomly generate linear equations. After the equations are generated I would then like to randomly insert the 7 fixed reference points into the equations and calculate the results. I currently have several programs that can generate random string of words from a file that contains a list of word but is not much help creating random equations. Do you know if there is such a program that can do what I am trying to get accomplished?? Thanks Frank Hopkins Houston, Tx **Make your life easier with all your friends, email, and favorite sites in one place. Try it now. (http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom0010) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random equation generator
When you say linear, I'm assuming fitting y=mx+c, and passing through points? The line through points (x1, y1) and (x2,y2) is y - y1 = (y2-y1) / (x2-x1) * (x-x1) That multiplies out to: y = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) + y1 That gives m = (y2-y1)/(x2-x1) and c = y1 - (y2-y1)/(x2-x1) you can then create a class to represent the line: class Line(object): def __init__(self, (x1,y1), (x2,y2)): "create a line passing through points (x1,y1) and (x2,y2) (inputted as tuples)" self.m = (y2-y1)/(x2-x1) self.c = y1 - self.m def is_on_line(self,(x,y)): 'Test if point represtented by an (x,y) tuple is on the line" if self. m * x + self.c == y: return True else: return False def __str__(self): "returns the equation of the line in the form y=mx+c. Might be quite long if floats are involved." print "y=%dx + %d" % (self.m, self.c) Then all you have to do is choose a pair of points, then iterate over the list, testing each point in turn, wash, rinse and repeat. Does that help? On 4 Dec 2008, at 20:28, [EMAIL PROTECTED] wrote: I am starting out with 7 fixed reference points. From there I want a program that can randomly generate linear equations. After the equations are generated I would then like to randomly insert the 7 fixed reference points into the equations and calculate the results. I currently have several programs that can generate random string of words from a file that contains a list of word but is not much help creating random equations. Do you know if there is such a program that can do what I am trying to get accomplished?? Thanks Frank Hopkins Houston, Tx Make your life easier with all your friends, email, and favorite sites in one place. Try it now. ___ 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] best way to run several processes over and over
Hey all. I have a rather large app that uses 14 threads that all run at the same time. i use threading.Thread().start() to set them off. each one runs somthing like this def run(): while 1: do a bunch of stuff time.sleep(60) i have the option i guess of having fewer threads, and just calling an outside script for some of the actions, like this def run(): while 1: do a bunch of stuff time.sleep(60) os.system("run_other_scripts.py") Does one have an advantage over the other? thanks shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to run several processes over and over
"shawn bright" <[EMAIL PROTECTED]> wrote I have a rather large app that uses 14 threads that all run at the same time. each one runs somthing like this def run(): while 1: do a bunch of stuff time.sleep(60) i have the option i guess of having fewer threads, and just calling an outside script for some of the actions, like this def run(): while 1: do a bunch of stuff time.sleep(60) os.system("run_other_scripts.py") Does one have an advantage over the other? In general threads use a lot less resource than processes so your existing approach should be better. BUT the sleep(60) bothers me slightly. A one minute delay is an age in computer terms. It might be better to see if you can build the delay outside the threading and have each thread run just once then die. Then restart the threads after 60 seconds at the top level. But whether that is really any better would require you to try it and see I suspect. Guessing the effect of these kinds of changes is little more than speculation... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to a file problem....
Marty Pitts a écrit : Date: Fri, 5 Dec 2008 23:48:32 +0100 From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: [Tutor] Writing to a file problem zip_command = "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' '.join(source)) What if you just double the '\' -- you did it properly for you "j:\\..." path. denis I tried that as well a bunch of variations such as r'j:\Backup\ and 'j:\\Backup\\' and 'j:/Backup/' None of which seems to have worked. Sorry. Also, is the '!' really a valid part of a zip argument? Have you tried for a test e.g. c:\Users\Marty\Zip\Zip -!rv 'test_target' test_source.txt and c:\Users\Marty\Zip\Zip -!rv 'test_target' test_source1.txt test_source2.txt on the command line? (test_sources existing) If yes, meaning that the overall zip_command format is right, then logially the only source of noise precisely is 'source'. Obviously, it must be a sequence of valid name files. Try: print "%s\n%s\n%s" % ( source , ' '.join(source) , ( "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' '.join(source)) ) ) to really check how you call zip. There must be something wrong, no? By the way, I just checked the output format above, and it gave me: c:\Users\Marty\Zip\Zip -!rv 'test.txt' test1.text test2.txt How come that the target is surrounded with ''? For zip, all of that arg list is plain text anyway... Is it necessary for zip to identify target? Or for filenames that include spaces? I would try without apostrophes, anyway, and the contrary, meaning to quote source list, too: ' '.join([("'%s'" %name) for name in source]) (untested) (Now, I think that this last version has higher chances to be right, because of the space_in_file_name issue. Try to use "", too.) Hope this helps. denis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor