Software DevelopmentLearn Operating System Interaction in Python

Learn Operating System Interaction in Python

Python Tutorial – Advanced Topics

As nobody can live alone and isolated from others, the interaction with the surrounding environment is a necessity. This holds true both in real life, and in computer World. In your computer programs – and for a reason or another, you may need to check the date and time on the system, or to know the current directory. You may need to check the available disk space of a specific file system, or the number of available CPUs. You may simply need to check for the existence of a file or directory. In some situations, you would need to do more than just making checks. In other words, there will be cases when you need to make changes to the environment where you program runs. These changes could be: changing the value of an environment variable, creating a directory, changing to a directory, or executing a command on the system.

In this article, we are going to learn how Python programs can interact with the operating system. We will support the concept by illustrative examples on both Windows and Linux systems.

* * * * * *

The OS Package
The Python language has the built-in os module that provides means for interacting with the operating system. The module offers a long list of functions and attributes that help the programmer achieve many OS-related tasks.
Some of these functions and attributes are specific to UNIX (and Linux) distributions, while others are available for both UNIX and Windows systems.

Windows-Supported Functions

The environ attribute
This attribute has the complete set of environment variables in the system. The following shows its value on a Windows 7 desktop. You could easily identify the values for some attributes like the OS, Computer Name, The CPU Architecture, the username, the home directory of the user, the number of processors in the machine, and the Path.
1
The cpu_count Function
As its name tells, this function returns the number of CPUs in the system.
2
The getcwd Function
This function returns the current working directory.
3
The getlogin Function
This function returns the current logged in user.
4
The listdir Function
This function lists the contents of a Directory. If none specified, the contents of the current directory are listed.
5
The makedirs Function
This function creates the directory specified by the passed string argument.
6
The removedir Function
This function deletes the directory specified as argument.
7
The rename function
This function can be used to rename a file or a directory. In the following examples, a diretory whose name is python will be renamed to newpython. Similarly, a file named seq.py will be renamed to sequence.py.
8
The system Function
This function allows the user to execute a system command.
The following example will use the system function to run the calc.exe
9
Example
The following example uses the system function to ping the google’s website by sending 10 ICMP echo request packets.
10
UNIX-Supported Functions
The majority of the os package functions are written for UNIX systems. In this section, we are going to investigate some of the important functions and illustrate them with examples.

The name Attribute
Contains name of the operating system dependent module imported.
11
The environ Attribute
A string value that contains information about the current environment variables.
12
In the output shown above, we can identify:

  • This is an SSH connection from the client at IP address 192.168.1.4
  • The logname (user) for this session is root.
  • The value of the PATH variable.
  • The home directory of the current user.
  • The Language and the Terminal Type.
  • The Hostname of this server.

The chdir Function
As the name tells, this function changes the current directory.
13
The getcwd Function
This function returns the current working directory
14
The uname Function
Returns information about the operating system version, hostname, and the kernel version of this system.
15
The mkdir Function
Like its equivalent UNIX/Linux command does, the mkdir Function create a directory.
16
The chown Function
As its name implies, this function changes the ownership of a file or directory. It takes three arguments:

  • The path of the file or directory to change the ownership for.
  • The uid of the new owner.
  • The gid of the new group owner.

Example
In this example, the /tmp/newdir is owned by the user ahmed. We need to change its owner to sherif, while reserving the group ownership as it is.
17
Given that the user ID of the user sherif is 500, the following can achieve the requested change:
18
To verify the ownership of the directory has been changed:
19
The symlink Function
This function creates a symbolic link that points to a target file or directory. It is equivalent to the UNIX command ln –s
20
The kill Function
This function is used to send a signal to a specific process (given the PID of the process).

Example
In this example, we want to kill the PostgreSQL process. As shown in the figure below, its PID is 971. To kill the specified process, the kill function is used and provided two arguments:
The first: the process ID (pid) of the process to kill.
The Second: the SIGQUIT signal that kills the process.
21
Notice that the process doesn’t exist after using the kill function.

The system Function
The system function allows Python programs to run and execute system commands. For example, to get the current system date and time, the date command can be executed:
22
Example
In this example, we are going to start the PostgreSQL service that we have killed in the last example. To do this, we need to use the command service postgresql start
23
After, starting the service, we have also verified it is up and listening for incoming connections on port 5432. This was done by executing the system command netstat –tupln

* * * * * *

In this article, we have learned how Python programs can interact with the operating system. We have illustrated our work by examples. I hope you find this article useful.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -