Python os Module

The os Module

OS module comes under Python’s standard utility modules. The OS module provides a portable way of using operating system-dependent functionality.

Although the Python os Module description belongs on Python Modules page,. We will describe it here because we are going to use the os Module extensively in Sprite game program development.

The sections below explain how the os module is used in the programs and the motivation for using it.

The program examples use the statement below to import the os module.

import os

os.path Module

os.path module is a sub-module of the os module in Python. It is used for common pathname manipulation.

os.path.dirname(__file__)

Python provides a variable called __file__ that refers to the directory wherever your python programs are stored. The command os.path.dirname figures out the path to that directory. 

Below is a program example of how os.path.dirname(__file__) command finds the folder where your python .py program is located. 

In the program below, the .py program is located in the directory C:/Users/MyUserName/Python_scripts. Note how the os.path.dirname(__file__) figures out where the .py program is located.

PROGRAM EXAMPLE: os.path.dirname(__file__) Usage

Program Name:

# Script showing use of __file__ and os.path.dirname
import os
# Os.path.dirname (__ file__) returns the directory where
# .py program file saved.
pathName = os.path.dirname(__file__)
print("Path of the .py file is: ", pathName)

Below is the program’s output.

>>> 
========== RESTART: C:/Users/MyUserName/Python_scripts/sprite_os_example.py ==========
game_folder\images
Path of the .py file is:  C:/Users/MyUserName/Python_scripts

This program was stored in the directory /MyUserName/Python_scripts folder. Note that the command os.path.dirname figured out the path to that directory.

os.path.join Method

Syntax: os.path.join(path*paths)

This method joins one or more path components intelligently. The return value is the concatenation of the path and any members of *paths with exactly one directory separator following each non-empty part, except the last, meaning that the result will only end in a separator if the last part is empty.

If a component is an absolute path, all previous components are thrown away, and joining continues from the absolute path component. Below is a brief program showing the usage of os.path.join(path) method.  In the example below, the .py programs are stored in a folder named “sprite_program_folder” and the image is stored in a folder named “sprite_images”.

PROGRAM EXAMPLE: os.path.join Method to Join Path Components

Program Name: sprite_os_join_example

# Example to show how os.path.join method works.
# to join various path components.
# In this example, the "sprite_program_folder" is
# joined with "sprite_images" folder"
# by putting a \ directory separator between the paths names.
import os
path = "sprite_program_folder"  # path for folder that contains Python programs.
my_img_folder = (os.path.join(path, "sprite_images"))
# "sprite_images" folder contains sprite images.
print(my_img_folder)
print("Joined path is: ", (os.path.join(path, "sprite_images")))

Below is the program’s output.

>>> 
========== RESTART: C:/Users/MyUserName/Python_scripts/sprite_os_example.py ==========
sprite_program_folder\sprite_images
Joined path is:  sprite_program_folder\sprite_images

Note that the program joined the “sprite_program_folder” with the “sprite_images” folder putting a “\” directory separator between the two path components.

Some Additional Functions Used in Sprite Programs

We have not introduced the following functions so far. Let us review these now before we get to sprite game programming.

convert() Function

When your program needs to display the image, you first need to load the image by using a statement such as:

Example:

Shuttle_img1 = pygame.image.load('SpaceShuttle.png'))

In the above statement, the load function takes the filename “SpaceShuttle.png” and returns a new Surface with the loaded image named “Shuttle_img1”.

After the image is loaded to the surface, we make a call to the Surface method, convert(). The convert() method returns a new surface of the image converted to the same pixel format as our display screen. Since the Surface with loaded image and the display screen is in the same format, the blitting is faster. Use Convert() method to make the blitting faster.

Shuttle_img = Shuttle_img1.convert()

The above statement takes the SurfaceShuttle_img1 and converts it to new surfaceShuttle_img.

pygame.set_colorkey() Function

When you download an image or create an image, it always has a background in a rectangular shape that contains the image. This rectangle may have a different color (white, black, or some other color). Typically, we do not want to show these background pixels in the rectangle around the image on the pygame screen. Pygame provides a function called colorkey() to ignore these background pixels. The color border you want to get rid of is typed within the parenthesis.

Syntax: set_colorkey(Color, flags=0)

Color is the background color in the surrounding rectangle of the image. The optional flags argument can be ignored for our discussion.

The statement set_colorkey(Color) sets the current color key for the Surface. When blitting this Surface onto a destination, the pixels that have the same Color as the color specified within the parenthesis of the set_colorkey(Color) statement will be transparent.

set_colorkey() just tells Pygame that when we draw the image we want to ignore any pixels of the specified color.

Example: myImage.set_colorkey(BLACK)

The above statement will make the BLACK color transparent in image “myImage”.

Copyright © 2019 – 2021 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights