G+_Russ DiBennetto Posted May 13, 2014 Share Posted May 13, 2014 Simple example on Importing Modules to make code independent of OS platform. Feel free to use this example on the show. #------------------------------------------------------------------------------- # Name: module1 # Purpose: # # Author: rdibened # # Created: 04/02/2014 # Copyright: © rdibened 2014 # Licence: #------------------------------------------------------------------------------- import os import platform import sys def main(): pass if _name_ == '__main__': main() if(platform.system() == 'Linux'): os.system('ls') if (platform.system() == 'Windows' ): os.system('dir') key=raw_input("Depress enter to exit") Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted May 13, 2014 Share Posted May 13, 2014 Better than detecting the OS and doing different things, is to use the language's library functions to do things portably. In this case, is.readdir() fetches a directory list that you can then print, and doesn't require platform. You should never import platform unless you have failed to find a portable function in some other library. Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted May 14, 2014 Share Posted May 14, 2014 What's the purpose of the empty main() function? Is it just a placeholder for initialization code? Link to comment Share on other sites More sharing options...
G+_Russ DiBennetto Posted May 14, 2014 Author Share Posted May 14, 2014 Lee Crocker Interesting, like I said, I have been programming in Python for only a few months. Something I might look into in the future. Right now for me trying to learn Python, I try not to complicate my command set, (too much to remember). I wanted to also put up a simple example for the show. Link to comment Share on other sites More sharing options...
G+_Russ DiBennetto Posted May 14, 2014 Author Share Posted May 14, 2014 Darryl Medley I use Pyscripter to develop my Python programs on a Windows platform. Pytscripter is a free IDE. When you start a new module it automatically puts that code in there. It is superfluous but most of the time I just leave it in there. Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted May 14, 2014 Share Posted May 14, 2014 I didn't know about Pyscripter. Thanks for the info. What advantages does it have over IDLE? Link to comment Share on other sites More sharing options...
G+_Jon Melbo Posted March 4, 2015 Share Posted March 4, 2015 OP is a bit old but I'll comment anyway... one advantage is that it does not force you to save your source code before you can run it. I think that is a valuable feature. I may not want to save the source if something I just added doesn't work right. Another advantage is a nicely organized visual interface. IDLE makes you jumble separate windows to the foreground as you wish to see them. Link to comment Share on other sites More sharing options...
Recommended Posts