AppDividend
Latest Code Tutorials

Python OS Module Example | OS Module In Python 3

0

Python OS module provides a way of using operating system dependent functionality. The functions that the OS module provides allow you to interface with the underlying operating system that Python is running on: be that Windows, Mac or Linux.

If you just want to read or write a file then use the open() function, if you want to manipulate paths, then use the os.path module, and if you want to read all the lines in all the files on the command line then use the fileinput module. For creating temporary files and directories, use the tempfile module, and for high-level file and directory, handling see the shutil module.

The functions OS module provides us for operating on underlying Operating System tasks, irrespective of it being a Windows Platform, Macintosh or Linux.

Python OS Module Example

Content Overview

The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. The os module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.

The os module is the part of the standard library, or stdlib, within Python 3. This means that it comes with your Python installation, but you still must import it.

Write the following code to import the OS module. If you do not know what is a module in Python, then you can check out this Python Module article.

import os

Now, let’s see some of the essential functions of os in detail.

#os.name

The os.name function gives the name of the OS module it imports. This differs based on the underlying Operating System. 

# app.py

import os

print(os.name)

See the output.

Python OS Module Tutorial

#os.environ

The environ is not a function but a process parameter through which we can access the environment variables of the system.

Let’s see the following example.

import os

print(os.environ)

See the output.

Python OS environ

We can also print the HOME environment.

# app.py

import os

print(os.environ['HOME'])

See the output.

Python OS environment example

#os.getcwd()

The getcwd function of the OS module will give us the current directory of the project.

# app.py

import os

print(os.getcwd())

See the output.

Python OS getcwd example

If you want to make a new directory type the following code.

# app.py

import os

os.mkdir('newDir')

It will create a new directory called newDir inside the current folder.

#os.execvp()

The execvp function is one of the ways to run other commands on the system. Let’s see the following example.

Create one file inside the same folder called mod.py and add the following code.

# mod.py

student = {
  'name': 'Krunal',
  'enno': 21,
  'college': 'vvp college'
}
print(student)

Now, write the following code inside the app.py file.

# app.py

import os
program = 'python'
arguments = ['mod.py']
print(os.execvp(program, (program,) + tuple(arguments)))

Now, run the app.py file.

Output

Python os execvp example

#os.getgid()

It returns the real group id of the current process.

# app.py

import os

print(os.getgid())

It returns the 20. That means the group id of the current process is 20.

#os.getuid()

The os.getuid os module function returns the current process’s user ID or UID, as it is popularly known.

See the following example.

import os

print(os.getuid())

It returns the 501.

#os.getpid()

The os.getpid returns the process ID of the current process.

# app.py

import os

print(os.getpid())

Output

Python os getpid example

#os.system

Python os system function allows us to run a command in the Python script, just as if I was running it in my shell. See the below example.

# app.py

import os

newFile = os.system('users > app.txt')

If you run the above app.py file, the new file is created called app.txt and inside that krunal is written because I am the user of my computer. You can see your name.

There are so many other OS modules that you can use in your project as per your requirement.

#os.error

All functions in this module raise OSError in the event of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not trusted by the operating system. Python os.error is an alias for built-in OSError exception.

#app.py

import os
try:
    filename = 'omg.txt'
    f = open(filename, 'rU')
    text = f.read()
    f.close()

except IOError:
    print('Problem reading: ' + filename)

Output

➜  pyt python3 app.py
Problem reading: omg.txt
➜  pyt

Finally, Python OS Module Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.