Python os Library Functions

Filed Under: Python

Python OS module allows us to use the operating system dependent functionalities and to interact with the underlying operating system in several different ways. For example, we can work with files, change the environment variables, and we can move files around, etc. This is as same as overriding all the os built-in functionalities in a module and using them in a file I/O and system handling.

Python import os

Now, as it is a built-in module, so we don’t have to install any third-party libraries. We can import the os module in our program like this:


import os  # importing the complete os module

from os import name  # importing a variable from the os module

Let’s look at a simple example of using the os module.

Code:

import os

print(dir(os))

Output:

Python Os Module

Python Os Module

NOTE: By printing this built-in dir() function and pass the os module, it shows us all of the attributes and methods that we have access to within this module.

OS Module Common Functions

OS module provides some callable methods and some variables. Some of the common methods for different functional categories are:

  1. Manipulating directories:
  • chdir()
  • getcwd()
  • listdir()
  • mkdir()
  • makedirs()
  • rmdir()
  • removedirs()
  1. Removing a file:
  • remove()
  1. Renaming files/directories:
  • rename()
  1. Using more than one process:
  • system()
  • popen()
  • close()
  • walk()
  1. User id and process id:
  • getgid(), os.getuid(), os.getpid()
  1. More about directories and files:
  • error
  • stat()
  1. Cross-plateform os attributes:
  • name
  1. Accessing environment variables:
  • environ

Common Functions Explanation and Usage

  1. os.name: This is the name of the imported operating system dependent module. Some of the registered module are – ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’.

print(os.name)

Output:

Os Name

Os Name

  1. os.error: It is environment error class for I/O errors and OSError and is raised when any function returns any system-related error. Each of the or module functions returns these errors when any invalid or inaccessible file is triggered in the line-of-code.

import os

try:
    filename = 'abcd2.txt'
    f = open(filename, 'r')
    text = f.read()
    f.close()

except os.error:

    print('Problem reading: ' + filename)

Output:

Os Error

Os Error

  1. os.system(): executing a shell command.
 cmd = "git --version"

returned_value = os.system(cmd)  # returns the exit code in unix
print('returned value:', returned_value)

­
OUTPUT:

Linux os.system example

Linux os.system example

  1. os.environ(): It is known as the value of an object which returns all the directories of all user environment variables .E.g. ‘HOME’-directory environment variables
import os

os.chdir('C:/Users/user/Desktop/temp')
# returns all the environment variables
print(os.environ)
# to get in particular
print(os.environ.get('TEMP'))

OUTPUT:

Linux os.environ Example

Linux os.environ Example

  1. os.getcwd(): returns the current working directory(CWD) in which the user is currently.
                    
print(os.getcwd())

OUTPUT:  C:\Users\user\.PyCharmCE2017.3\config\scratches

  1. os.chdir(): to change the directory.

os.chdir('C:/Users/user/.PyCharmCE2017.3/')

print(os.getcwd())

OUTPUT:  C:\Users\user\.PyCharmCE2017.3

  1. os.listdir(): It returns a list of files and the folders in the current directory.

print(os.listdir())

OUTPUT: ['.git', '1802.04103.pdf', '1st year', '2K16-CO-200', 'abc.txt', 'afcat',]

  1. os.popen(command[, mode[, bufsize]]) : it opens a pipe to or from command. It returns an open file object connected to the pipe, which can be read or written depending on whether the mode is ‘r’ (default) or ‘w’.

import os

fd = "abc.txt"

# popen() is similar to open()
file = open(fd, 'w')
file.write("Hello")
file.close()
file = open(fd, 'r')
text = file.read()
print(text)

# popen()and accesses the file directly
file = os.popen(fd, 'w')
file.write("Hello")

OUTPUT:

Os Popen

Os Popen

Os Popen Notepad

Os Popen Notepad

  1. os.close(): Close file descriptor fd.

Note: It must be applied to a file descriptor returned by os.open() or pipe() built-in functions of os module.

 fd = "abc.txt"
file = open(fd, 'r')
text = file.read()
print(text)
os.close(file)

OUTPUT:

Os Close

Os Close

NOTE: Error is thrown for the non-existence of the file or the permission privileges.

  1. os.getgid(), os.getuid(), os.getpid(), and os.stat(): getgid() returns the real group id for the current process. The getuid() function returns the current process’s user id and getpid() returns real process id of the current process. The os.stat() function returns the list of details about the file or the directory name given in the argument.

print(os.stat('abcd1'))

# for some simplified and particular details
# we can use dot operator and that attribute name
# this returns the timestamp of last modification time
print(os.stat('abcd1').st_mtime)

# this returns the size of the file in bytes
print(os.stat('abcd1').st_size)

OUTPUT:

Linux os.stat Example

Linux os.stat Example

  1. os.walk(): it is a generator that yields a couple of three values as it is walking the directory tree and for each directory that it traverses and produces the directory path, the direct within that path and the files within that path. It is useful to keep track of all the directories.

os.chdir('C:/Users/user/Desktop/temp')

# returns a 3-tuple
for dirpath, dirname, filename in os.walk('C:/Users/user/Desktop/temp'):
    print('Current path: ',dirpath)
    print('Directories: ', dirname)
    print('Files: ', filename)
    print()

OUTPUT:

Os Walk

Os Walk

  1. os.mkdir() and os.makedirs(): To create new directories.

Differences: makedirs() creates all the intermediate directories if they don’t exist already and mkdir() can create a single sub-directory and will throw an exception if intermediate directories that don’t exist are specified.

OUTPUT:


['.git', '1802.04103.pdf', '1st year', '2K16-CO-200', 'abc.txt', 'abcd', 'abcd1', 'afcat',]

Traceback (most recent call last):

File "C:/Users/user/.PyCharmCE2017.3/config/scratches/scratch.py", line 18, in <module>

os.mkdir('abcd2/subdir')

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'abcd2/subdir'

['.git', '1802.04103.pdf', '1st year', '2K16-CO-200', 'abc.txt', 'abcd', 'abcd1', 'abcd2', 'afcat']

Process finished with exit code 1

NOTE: The line 18 code thrown an error for creating a directory along with a subdirectory because os.mkdir() don’t work in a tree fashion.

Os Mkdir

Python os mkdir() Function

  1. os.rmdir() and os.removedirs(): same as os.mkdir() and os.makedirs() os.rmdir() will not remove the intermediate directory where as os.removedirs() will remove the intermediate directories. Observe the below code and output as continued with previous commands and directories.

os.rmdir('abcd')
os.removedirs('abcd2/subdir')

print(os.listdir())

OUTPUT:  [‘.git’, ‘1802.04103.pdf’, ‘1st year’, ‘2K16-CO-200’, ‘abc.txt’, ‘abcd1’, , ‘afcat’]

  1. os.rename():to rename a file or a folder.In arguments pass the original file name first and then the new name of the file.
os.chdir('C:/Users/user/Desktop/temp')

print(os.listdir())

os.rename('xyz.txt','abc.txt')

print(os.listdir())

OUTPUT:

Os Rename

Python os rename() function

  1. os.remove(): It removes the path of a file. It takes path string as a variable.
import os

os.chdir('C:/Users/user/Desktop/temp')
print(os.listdir())
os.chdir('C:/Users/user/Desktop/')

os.remove('temp/abc.txt')
os.chdir('C:/Users/user/Desktop/temp')

print(os.listdir())

OUTPUT:

Os Remove

Python os remove() Function

Advantages of Using Python OS Module

  • This module is useful if you want to make your programs platform-independent, i.e. with the use of python os module you can make your code run smoothly on linux as well as on windows without requiring any changes to be made.
  • It represents the generic system functionality.

Conclusion

At this point will be familiar with the python os module. In this article, we  learned the following:

  • how to work with environment variables
  • change directories and discover your current working directory
  • create and remove folders and files
  • rename files / folders
  • start a file with its associated application
  • walk a directory
  • work with paths

There are a lot of other functions in the os module that has not been discussed here because they are not commonly used. Be sure to read the documentation https://docs.python.org/3/library/os.html to see what else you can do with the python os module

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages