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
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.
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.
As you can see, sys will clearly print out your platform and version (processor, and a bunch of other stuff I can't understand)
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.
It will be more clear as to why this is important later in the tutorial (platform specific tools).
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.
The first line here gives us the current working directory of the program, the rest is above my head =p.
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.
This is basically just another way of changing your PYTHONPATH shell variable, but not very effective because the folder will only remain there until the end of your shell session or program.
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.
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
>>> sys.platform, sys.version
('win32', '2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]')
>>>
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
>>> if sys.platform[:3] == 'win': #checks if the first 3 letters are "win"
print 'This is windows!'
else:
print 'This isnt windows!'
This is windows!
>>>
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
['C:\\Documents and Settings\\Administrator\\Desktop\\Coding\\Python\\Tutorials\\examples', 'C:\\Python26\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
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
>>> sys.path
['C:\\Documents and Settings\\Administrator\\Desktop\\Coding\\Python\\Tutorials\\examples', (deleted for the sake of space) 'C:\\modules'] #There is our folder!
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.
To be honest I was going to post the os.environ.values(), but the list was just too massive. These are a list of shell variabels that currently exist on my computer, lets look at some of the keys.
These variable can be changed as well!
Lets make a shell variable of our own and show how it can be implemented in our programs:
You can write programs that reference to a certain shell variable, and if it is not what the program expects, the program can change it, or just die.
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
os.path.exists will basically verify whether or not a path indeed exists. Any program that prompts for a path needs to know whether or not it exists.
Now lets take a look at this program once it is run
os.path.split will split the directory and the file into two distinct strings. For example:
os.path.join will put the file and directory strings back together.
This is useful if your program calls for a file to be placed into the cwd, you can easily create the path by joining the cwd with the name of the file. Now lets look at os.path.basename, os.path.dirname, and os.path.splitext. For all of these examples, we will be using C:\example1.py.
dirname and basename work the same as os.path.split, except they are each a standalone string and are therefore(as far as I can see) more useful. os.path.splitext works the same as os.path.split except it returns the dirctory/filename and then the extension in two separate strings.
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.
As you can see, they are exactly the same thing, until we change our PYTHONPATH, then this is the output.
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
>>> os.environ.keys()
['TMP', 'COMPUTERNAME', 'USERDOMAIN', 'COMMONPROGRAMFILES', 'PROCESSOR_IDENTIFIER', 'PROGRAMFILES', 'PROCESSOR_REVISION', 'SYSTEMROOT', 'PATH', 'TK_LIBRARY', 'TEMP', 'PROCESSOR_ARCHITECTURE', 'TIX_LIBRARY', 'ALLUSERSPROFILE', 'SESSIONNAME', 'HOMEPATH', 'USERNAME', 'LOGONSERVER', 'COMSPEC', 'QTJAVA', 'CLASSPATH', 'TCL_LIBRARY', 'PATHEXT', 'FP_NO_HOST_CHECK', 'WINDIR', 'APPDATA', 'HOMEDRIVE', 'SYSTEMDRIVE', 'NUMBER_OF_PROCESSORS', 'PROCESSOR_LEVEL', 'OS', 'USERPROFILE']
Code:
>>> os.environ['PROCESSOR_REVISION'] #key
'0304' # value
>>> os.environ['TMP']
'C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp'
>>> os.environ['TEMP']
'C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp'
Code:
>>> os.environ['TEMP'] = r'C:\temporary' #remember to use the r
>>> os.environ['TEMP']
'C:\\temporary'
Code:
>>> os.environ['USER'] = 'Devin' #Thats my name in case you were wondering
>>> print 'Hello %s, how are you' %(os.environ['USER'])
Hello Devin, how are you
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)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_getfullpathname', 'abspath', 'altsep', 'basename', 'commonprefix', 'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 'join', 'lexists', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath', 'relpath', 'sep', 'split', 'splitdrive', 'splitext', 'splitunc', 'stat', 'supports_unicode_filenames', 'sys', 'walk', 'warnings']
Code:
import os
x = raw_input('Give me a correct path, NOW!')
if os.path.exists(x):
print 'Thank you, this path is correct'
if not os.path.exists(x):
print 'This path does not exist, YOU FAIL'
Code:
>>>
Give me a correct path, NOW!C:\Python26
Thank you, this path is correct
>>>
Give me a correct path, NOW!Support Forums is pretty awesome
This path does not exist, YOU FAIL
>>>
Code:
>>> os.path.split(r'C:\example1.py')
('C:\\', 'example1.py')
Code:
>>> os.path.join('C:\\', 'example1.py')
'C:\\example1.py'
Code:
>>> os.path.basename('C:\example1.py')
'example1.py'
>>> os.path.dirname('C:\example1.py')
'C:\\'
>>> os.path.splitext('C:\example1.py')
('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
print 'My cwd is: \n %s' %(os.getcwd())
print ' My sys path is: \n %s' %(sys.path[:2]) #prints only the first 2 sys.path
raw_input()
Code:
C:\examples\test>set PYTHONPATH=C:/examples
C:\examples\test>python example1.py
My cwd is:
C:\examples\test
My sys path is:
['C:\\examples\\test', 'C:\\examples']