Introduction to System Tools - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Python Programming Language (https://www.supportforums.net/forumdisplay.php?fid=32) +---- Thread: Introduction to System Tools (/showthread.php?tid=4510) |
Introduction to System Tools - uber1337 - 01-25-2010 Well, everyone else is making a tutorial, so I decided to make one too! This tutorial is pretty short and really just something I threw together in a couple of hours. This is clearly meant for beginners so don't get all mad if all of this is too easy for you. If you spot a mistake or a misinterpretation, please tell me, I am currently learning it so this tutorial might fail to cover important topics. [size=large (Click to View) Table of contents:[/size]] Introduction 1.0 sys Module 2.0 Platforms/Versions 2.1 Search Path 2.2 os Module 3.0 Shell variables 3.1 os.path 3.2 [size=large (Click to View) Introduction 1.0[/size]] Python comes with built in modules that deal with your computer and perform administrative tasks, tell you basic things about your computer, etc. The two main modules we will be looking at are os and sys. For a list of attributes open the IDLE and type dir(os) or dir(sys). This tutorial will introduce the basics of system tools in Python and is intended to help beginners grasp the idea of using Python in relation to your computer. More advanced Python users may find this tutorial a little slow but bear with me as I am just a beginner myself. [size=large (Click to View) The sys module 2.0[/size]] First we will start with the smaller of the two modules, sys, for a list of sys's attributes, type dir(sys). This module can prove to be very handy when dealing with standard input, standard output, and standard error streams (which we will get to later). Platforms and Versions 2.1 First lets take a look at platforms using the sys module. Code: >>> import sys How can this help you? Lets say you write a program that deals with the users computer, but your program depends on the operating system being windows. Code: >>> import sys Search Path 2.2 Now we will be using sys.path. Running sys.path will give us the cwd ( Current working directory) and a bunch of other important folders being used, lets take a look. Code: >>> sys.path Every time you import something, the interpreter will scan all the directories shown above for the file specified. Surprisingly enough, sys.path can be changed by a program! This can come in handy when you have a folder with all the modules you created, but you are too lazy to move them to a python directory or the cwd. Code: >>> sys.path.append(r'C:\modules') #adds another directory for python to look at Note: In (r'C:\modules'), the “r” is necessary because “\” is seen as the beginning of an escape code, but adding the “r” tells python it is a raw string. (another way to do it is by adding 2 backslashes, ie: C://modules) This is the end of the sys module, (not really). These are some of the basic uses and how they can be implemented, some more important things such as stdin, stdout, and stderr deserve there own section and will be seen later in the tutorial. [size=large (Click to View) The os module 3.0[/size]] Here we will introduce the biggest of the two modules, os. This module is perhaps the more important of the two and will probably be used a whole lot more (especially by linux users). We will explore how the module deals with the shell, paths, and cwd(current working directory). Shell variables 3.1 First comes first, shell variables. Python deals with shell variables using os.environ. You should already know about pythons shell variable PYTHONPATH, Python uses it to know where to look for modules and what not. Shell variables can also be set by programs, and later used as the input source for other programs(interprocess communication). Think of os.environ as a python dictionary, an associative array of keys and values, lets take a look. Code: >>> import os Code: >>> os.environ['PROCESSOR_REVISION'] #key Code: >>> os.environ['TEMP'] = r'C:\temporary' #remember to use the r Code: >>> os.environ['USER'] = 'Devin' #Thats my name in case you were wondering os.path 3.2 As my book puts it, the “os.path to life”, os.path is a very useful tool when dealing with paths to files, etc. Lets take a quick look at some of its attributes Code: >>> dir(os.path) Code: import os Code: >>> Code: >>> os.path.split(r'C:\example1.py') Code: >>> os.path.join('C:\\', 'example1.py') Code: >>> os.path.basename('C:\example1.py') Now we will look at os.getcwd(). It actually is not part of os.path, but it is too short of a topic to make its own section for, so I will cover it here. Code: import os, sys Code: C:\examples\test>set PYTHONPATH=C:/examples RE: Introduction to System Tools - Nyx- - 01-25-2010 way to go ^__^ |