G+_Russ DiBennetto Posted May 13, 2014 Share Posted May 13, 2014 I have been programming in Python for about 8 months. I have a Raspberry Pi and I am running a public domain program called JCblock on it. I wanted to build a front end to it. Here is a module I wrote to sort the Blacklist.dat file. I incorporated this into a GUI using Tkinter. I thought I would share the code with you and a bit of the file that it sorts. #------------------------------------------------------------------------------- # Name: module1 # Purpose: # # Author: rdibened # # Created: 21/09/2013 # Copyright: © rdibened 2013 # Licence: #------------------------------------------------------------------------------- def main(): pass if _name_ == '__main__': main() fbuff = open("blacklist.dat" , "r" ) lines=fbuff.readlines() lines.sort() fbuff.close() fbuff = open("blacklist.dat" , "w" ) fbuff.writelines(lines) fbuff.close() print("Done") Now for a segment of the file # 01 The first field is the one tested. It can be the telephone # 02 number or some other field in the caller ID string. It *must # 03 be* terminated with a '?' character. The Date-of-last-block # 04 field starts in the 20th column. Be sure to add the '111813' # 05 characters to a new entry so that room to store the date is # 06 present on the line. The rest of the line (out to 80 chars) # 07 can contain a comment of your choosing. # 08 *****Do not use tabs in the entries!******** # 09 Examples (without the initial '#' chars!): # 10 8005551212? 111813 JUNK CALL # 11 Cell Phone? 111813 (A cell phone call) # 12 | <---- the 20th column # 14 Tested field: Date field: Comment field: #O--NAME = ? 103013 Test for unavailable 0000000000? 102313 0824305456? 021914 1111111111? 111813 1407987523? 111813 1412530517? 111813 Enjoy. It works like a charm. I since added information to the code that determines if the system it is running on is Windows or Linux and changes the commands so it executes the proper syntax. For instance to delete a file in Windows it will send DOS a "del" command and if the program determines it is running in Linux it will execute a Bash "rm" command. This makes my program OS independent without changing code. If you would like to see an example of this code, let me know. Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted May 13, 2014 Share Posted May 13, 2014 The portable function os.remove() will delete a file. It doesn't require detecting the OS, and it doesn't waste time calling an external program. Link to comment Share on other sites More sharing options...
G+_L I Posted May 14, 2014 Share Posted May 14, 2014 I've seen this def main(): in a few programs now and I'm wondering what the thinking is behind it. Is it just a style thing to organize everything into functions? Also the if _name_ == '___main___': Is that to prevent code from running if this is imported as a module into another program? Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted May 14, 2014 Share Posted May 14, 2014 Yeah, the "if _name_" thing lets you write one file that can be run as a program or imported as a module. I have no idea why he adds the empty main(). Link to comment Share on other sites More sharing options...
G+_Russ DiBennetto Posted May 14, 2014 Author Share Posted May 14, 2014 the If_name is auto added my my Pyscripter IDE that I use for development on my windows system, it is superfluous to the code. Link to comment Share on other sites More sharing options...
Recommended Posts