[Tutor] question about looping.
Hi, I at trying to create a bunch of text files in a single directory on a Linux system, something like this. import os routers = ['adnc-6321', 'adnp-2341', 'adnw-2632'] for i in routers: os.system('/bin/touch' %s) % i of course this dosn't work. Is there a simple way to get this done? Thanks for your time. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Trying tio emulate diff command of UNIX - please help
# This program emulates the diff command of UNIX import sysfrom stringCompare import stringcmp # this is a module which has stringcmp function that compares two strings fname1 = raw_input(Enter a file name to be read:\t) fname2 = raw_input(Enter a file name to be read:\t) fd1 = open(fname1,r)fd2 = open(fname2,r) done = 0line_counter = 0 while not done: aLine1 = fd1.readline() aLine2 = fd2.readline() if (aLine1 == or aLine2 == ): # test whether you have reached the end of file done = 1 else: line_counter += 1# get the line number string1 = aLine1.split() # split the line into a listcontaining words string2 = aLine2.split () len1 = len(string1) len2 = len(string2) if len1 len2: t = len1 else: t = len2 i = 0 while (i t): cmp_res = stringcmp(string1[i],string2[i]) if cmp_res != 0: column = i done = 1 print The difference is lies in the , line_counter ,line and column , column Can someone help me with what is wrong in this code; when I am running it gets stuck. thanks in anticipation. Regards, Asrar -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying tio emulate diff command of UNIX - please help
Hi, Your program does not emulate the diff command of Unix. Please do a diff in unix and experience yourselves. Where is cmp_res = stringcmp(string1[i],string2[i]) stringcmp() function written? Moreover, if you Python Documentation install (orpython.org accessible) search for difflib and Differ Example. That should give you a good start. Thanks, -- Senthil From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Asrarahmed KadriSent: Friday, October 06, 2006 6:09 PMTo: tutor@python.orgSubject: [Tutor] Trying tio emulate "diff" command of UNIX - please help # This program emulates the diff command of UNIX import sysfrom stringCompare import stringcmp # this is a module which has stringcmp function that compares two strings fname1 = raw_input("Enter a file name to be read:\t") fname2 = raw_input("Enter a file name to be read:\t") fd1 = open(fname1,"r")fd2 = open(fname2,"r") done = 0line_counter = 0 while not done: aLine1 = fd1.readline() aLine2 = fd2.readline() if (aLine1 == "" or aLine2 == ""): # test whether you have reached the end of file done = 1 else: line_counter += 1# get the line number string1 = aLine1.split() # split the line into a listcontaining words string2 = aLine2.split () len1 = len(string1) len2 = len(string2) if len1 len2: t = len1 else: t = len2 i = 0 while (i t): cmp_res = stringcmp(string1[i],string2[i]) if cmp_res != 0: column = i done = 1 print "The difference is lies in the ", line_counter ,"line and column ", column Can someone help me with what is wrong in this code; when I am running it gets stuck. thanks in anticipation. Regards, Asrar -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about looping.
Doug Potter wrote: Hi, I at trying to create a bunch of text files in a single directory on a Linux system, something like this. import os routers = ['adnc-6321', 'adnp-2341', 'adnw-2632'] for i in routers: os.system('/bin/touch' %s) % i of course this dosn't work. try using the following: for i in routers: os.system('/bin/touch %s' % i) Is there a simple way to get this done? You can also use the builtin file object: for i in routers: f = file(i, 'w') f.close() Thanks for your time. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Carlos Hanson Web Specialist Tigard-Tualatin School District 503.431.4053 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying tio emulate diff command of UNIX - please help
Some general thoughts: import sys from stringCompare import stringcmp # this is a module which has stringcmp fname1 = raw_input(Enter a file name to be read:\t) fname2 = raw_input(Enter a file name to be read:\t) fd1 = open(fname1,r) fd2 = open(fname2,r) done = False Use Boolean value instead of 1/0 line_counter = 0 while not done: aLine1 = fd1.readline() aLine2 = fd2.readline() if (aLine1 == or aLine2 == ): # test whether you have reached the end of file done = 1 assign directly to done, using a boolean test: done = not (aLine1 and aLine2) and miss out the if/else test. line_counter += 1 # get the line number string1 = aLine1.split() # split the line into a list containing words string2 = aLine2.split() Not sure why you are splitting the lines into words. You call them columns but they are really words of varying length. Why not just compare the lines as a whole? if aLine1 != aLine2: print The difference lies in line, line_counter Then you can find the first characters that differ: diff = [t1==t2 for t1,t2 in zip(line1,line2)].index(False) print and start at character, diff It's not exactly equivalent to your code of course but it seems to me to be more accurate... If you must use words, apply the spolit only when you know its needed: words1 = aLine1.split() words2 = aLine2.split() diff = [w1==w2 for w1,w2 in zip(words1,words2)].index(False) print and start at word,diff 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
[Tutor] Help: how to detect which key is pressed
I am writing a simple program and in that I want to add some sort of interactiveness. I have designed a menu which gives the user an option to select one out of the following four keys: d, l, n or e. Can any one tell me how to determine which key has been pressed by the user. Thanks. Regards, Asrar-- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Share variables between py scripts
Bennett, Joe wrote: Can anyone direct me to some documentation on how to take variables from one py script, execute and pass them to another script? Then I need to get data back from the executed script? I've been searching Google and I see information, but I am in need of some examples... Thanks! -Joe Hi Joe, If possible, just run your script (B) from within the second script (A). I'd rephrase that as in module a, import and use variables, functions and/or classes from module b: # Script B (script_b.py) # my_b_variable = '' def my_b_function(var): return Hello World: %s % var class MyBClass: # an empty class pass End Script B # # Script A (script_a.py) # from script_b import my_b_variable, my_b_function, MyBClass def my_a_function(var): my_a_variable = MyBClass() # or my_a_variable = my_b_variable # or my_a_variable = my_b_function(var) # etc return my_A_variable if __name__ == '__main__': # this A script is being called from the command line # do whatever you like in here, like run a function: import sys command_line_arg = sys.argv[1] print my_A_function(command_line_arg) End Script A # $ python script_a.py foo Hello World: foo I hope that wasn't too remedial for you and that it's helpful. Search the docs for 'functions' and 'import' for more info. Best, Eric. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying tio emulate diff command of UNIX - please help
Thanks Alan, but can you please explain me what this line does: diff = [t1==t2 for t1,t2 in zip(line1,line2)].index(False) I'll unfold it somewhat: mix = zip(a,b) produces a list of tuples: [(a1,b1)(a2,b2),(a3,b3)] t1 == t2 produces a boolean result either True or False for t1,t2 in mix unpacks each tuple from the zipped list Putting it back together in long hand mix = zip(aLine1,aLine2) results = [] for t1,t2 in mix: if t1 == t2: results.append(True) else: results.append(False) diff = results.index(False) finds the index of the first instance of False in results Lets look at it as a comprehension again: diff = [t1==t2 for t1,t2 in zip(aLine1,aLine2)].index(False) Does that make sense now? Can you see the various bits at work? Alan G. ___ To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help: how to detect which key is pressed
Asrarahmed Kadri wrote: I am writing a simple program and in that I want to add some sort of interactiveness. I have designed a menu which gives the user an option to select one out of the following four keys: d, l, n or e. Can any one tell me how to determine which key has been pressed by the user. Asrarahmed: Please refer to: http://www.freenetpages.co.uk/hp/alan.gauld/ for information on how to do this. specifically, http://www.freenetpages.co.uk/hp/alan.gauld/tutinput.htm You should work through this tutorial, not just the specific section on User Input, but the whole site. It will cover the most common questions that people new to Python have. Good Luck, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor