Introduction to Python
The course is part of this learning path
In this first course, we introduce the Python Language, the declaration model, and how variables and functions are used in python.
Our learning objectives for this course are to introduce the python language and to be able to recognize and explain the core concepts of the Python language.
Hello and welcome to Python OS Services. Our learning objectives for this course are to learn to work with the OS, running external programs, walking through a directory tree, and working with path names. Now, the Python OS module provides many basic services from the operating system. These services include file and folder utilities as well as working with dates and times, running external programs, and many other functions. The OS module enables you to use operating system dependent functionality. There is no easy way to absorb all of these methods. You just have to become familiar with them. So let's just walk through them, and to give you a start point I'll call out some of the more frequently used methods.
The execute, or exec method, executes a file, with different configurations or arguments and environmental variables, et cetera. fchmod allows you to change permissions of files given by a file descriptor. fork is another common function, allows you to fork a child process. ftruncate allows you to truncate a file to a specified length. getenv, get specified environmental variables. getenvb gives you specified environmental variable or none/default, returns bytes. Difference, right, one's a string, one's bytes. kill, kill a process with a signal. lchown, change the owner or group of path without following sym links. mkdir, create a directory. open, open a file for low level IO. spawn, executes a file with arguments from args in a sub process. unsetenv, allows you to delete an environmental variable. wait, allows you to wait for completion of a child process. walk, allows you to walk a tree directory generator. And write, write a string to a file descriptor, paths, directories and file names. The OS path module provides many functions for working with file and directory names and paths. These are all about the file and directory's attributes, not about the contents. Some of the more common methods are: os.path.absolute path, basename, dirname, getmtime, getsize, isdir, isfile, join and exists.
So, in the current example we have, number one is the Unix relative path. Number two is the Unix absolute path. Number three is the Windows relative path. Number four is the Windows UNC path. Number five answers what platform are we on. Number six is just a folder name. Number seven, just the file or folder name. Number eight, is it an absolute path? The first tilde is current user's home and the second tilde is name's home. The os.walk method provides a way to easily walk a directory tree. It provides an at area for a directory and all its subdirectories. For each directory it returns a tuple with three values. The first element is the full absolute path to the directory. The second element is a list of the directory's subdirectories, with relative names. And the third element is a list of the none directory entities in the subdirectory, also their relative names. Be sure to use os.path.join to put together the directory and the file or subdirectory name. Don't use dir as a variable when looping through the iterator, because it will overwrite Python's built in dir function. There're several way to access environmental variables from Python. The most direct is to use os.environ which is a directory of the current environment. If a none existent variable name is specified, a key error will be raised. So, it is safer to use os.environ.get with the var name than it is to use os.environ with just the var name.
You can also use the os.getenv method. It takes the name of an environmental variable and returns that variable's value. An optional second argument provides a default value if the variable is not defined. Another way to use environmental variables is to expand a string that contains them. Using the expand var sting method of the os.path.object. This takes a string containing one or more environmental variables and returns the strings with environment variables expanded to their values. If the variables do not exist in the environment they are left unexpanded. In Python, you can launch an external command using the OS module functions os.system. os.system launches any external command as though you had typed it at a command prompt. popen opens a command returning a file-like object. You can read the output of the command with any of the methods used for a file. You can open a process for writing as well by specifying a mode of w. Okay, that brings us to the end of this course. Let's do a summary and then onto the next one.
Andrew is fanatical about helping business teams gain the maximum ROI possible from adopting, using, and optimizing Public Cloud Services. Having built 70+ Cloud Academy courses, Andrew has helped over 50,000 students master cloud computing by sharing the skills and experiences he gained during 20+ years leading digital teams in code and consulting. Before joining Cloud Academy, Andrew worked for AWS and for AWS technology partners Ooyala and Adobe.