Jump to content

Simple example on Importing Modules to make code independent of OS platform Feel free to use t...


G+_Russ DiBennetto
 Share

Recommended Posts

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

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

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

  • 9 months later...

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

 Share

×
×
  • Create New...