AppDividend
Latest Code Tutorials

Python OS Module Tutorial With Example

0

Python OS Module Tutorial With Example is today’s topic.  The OS module in Python provides a way of using operating system dependent functionality. The functions that the OS module provides allows you to interface with the underlying operating system that Python is running on – be that Windows, Mac or Linux. 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 Tutorial

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 below.

 

Python OS Module Tutorial

os.environ

The environ is not a function but a process parameter through which we can access 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 following output.

 

Python OS environment example

os.getcwd()

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

# app.py

import os

print(os.getcwd())

See the following 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.

 

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())

 

Python os getpid example

os.system

Python os system function allows us to run a command in the Python script, just like 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.

Finally, Python OS Module Tutorial With 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.