Python OS Module

  • The Python OS Module provides an interface with an underlying operating system that Python is running and it can be Windows, Mac or Linux.
  • It helps us to automate tasks like creating or removing directories

To get the current working directory

>>> os.getcwd()
'/home/cloud_user'

To change directory

>>> os.chdir("/tmp")

To verify it

>>> os.getcwd()
'/tmp'

To list directories

>>> os.listdir()
['systemd-private-929d26066ab64d9892496c0b5e05dbb6-systemd-timesyncd.service-JrC3MC', '.X1-lock', '.Test-unix', 'pulse-PKdhtXMmr18n', 'systemd-private-929d26066ab64d9892496c0b5e05dbb6-systemd-resolved.service-DXvPLp', '.X11-unix', '.XIM-unix', '.ICE-unix', 'ssh-54k5Trn8h27F', '.font-unix', 'systemd-private-929d26066ab64d9892496c0b5e05dbb6-rtkit-daemon.service-FethN5', '.xfsm-ICE-MJ54F0']

OR you can pass the path in the command

>>> os.listdir("/home/cloud_user")
['.profile', 'rest.yml', 'openssl', '.bash_history', '.kube', 'alpine.yml', '.ICEauthority', 'Documents', 'kubernetes-metrics-server', '.gvfs', 'Downloads', 'Music', 'kubernetes', '.bash_logout', 'regex', 'Public', '.viminfo', '.gnupg', 'Desktop', 'Videos', '.Xauthority', '.vnc', '.python_history', '.sudo_as_admin_successful', 'metrics-server', '.local', '.dbus', 'Pictures', '.mozilla', 'Templates', '.cache', '.config', 'multicontainer.yml', '.ssh', 'pod.yml', '.lesshst', '.bashrc']

To make directories

>>> os.mkdir("/tmp/testdir")

To create directory recursively

>>> os.makedirs("/tmp/a/b")

If you try to create directory recursively using os.mkdir you will get this error

>>> os.mkdir("/tmp/c/d")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/c/d'

To remove a file

>>> os.remove("pod.yml")

To remove a directory

>>> os.rmdir("/tmp/e")

To remove a directory recursively

>>> os.removedirs("/tmp/c/d")

To rename the file or directory

>>> os.rename("/tmp/xyz","/tmp/abc")

To get the environment variable information

>>> os.environ
environ({'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'LANG': 'C.UTF-8', 'XDG_SESSION_ID': '5', 'HUSHLOGIN': 'FALSE', 'USER': 'cloud_user', 'PWD': '/home/cloud_user', 'HOME': '/home/cloud_user', 'XDG_DATA_DIRS': '/usr/local/share:/usr/share:/var/lib/snapd/desktop', 'MAIL': '/var/mail/cloud_user', 'REMOTEHOST': 'localhost', 'SHELL': '/bin/bash', 'TERM': 'xterm-256color', 'SHLVL': '1', 'LOGNAME': 'cloud_user', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1001/bus', 'XDG_RUNTIME_DIR': '/run/user/1001', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin', 'LESSOPEN': '| /usr/bin/lesspipe %s', '_': '/usr/bin/python3'})

To get the userid

>>> os.getuid()
1001

To get the groupid

>>> os.getgid()
1001

To get the current shell process id

>>> os.getpid()
11377

To execute the shell command

>>> os.system("clear")

NOTE: If you try to save the output of the command in a variable, it will not work with os module , it only store the return code eg: 0 for successfully executed command in this case or non-zero for unsuccessful command.

>>> rt=os.system("ls")
Desktop  Documents  Downloads  Music  Pictures	Public	Templates  Videos  alpine.yml  kubernetes  kubernetes-metrics-server  metrics-server  multicontainer.yml  openssl  ospath.py  regex  rest.yml
>>> print(rt)
0

OS Path

  • os.path is a sub module of OS
  • os.path module is used to work on paths

Returns the final component of a pathname

>>> os.path.basename("/etc/ldap/ldap.conf")
'ldap.conf'

Returns the directory component of a pathname

>>> os.path.dirname("/etc/ldap/ldap.conf")
'/etc/ldap'

Join two or more pathname components

>>> os.path.join("/home","abc")
'/home/abc'

Let’s try to do the same with os.path.join,

>>> "home" + "abc"
'homeabc'

As you can, with os.path.join intelligently add the seperator between the two path based on the Operating System.

Split a pathname. Returns tuple “(head, tail)” where “tail” is everything after the final slash.

>>> os.path.split("/home/abc")
('/home', 'abc')

Return the size of a file

>>> os.path.getsize("/etc/ldap/ldap.conf")
332

NOTE: It returns the size in terms of bytes.

Test whether a path exists. Returns False for broken symbolic links

>>> os.path.exists("/etc/resolv.conf")
True

Test whether a path is a regular file

>>> os.path.isfile("/etc/resolv.conf")
True

Return true if the pathname refers to an existing directory

>>> os.path.isdir("/etc")
True

This is especially helpful, in checking if the file exists before performing any operation on the top of it.

import os
path="/etc/resolv.conf"

if os.path.exists(path):
    print("File exists")
else:
    print("File doesn't exists")

NOTE: I showed the above example using os.path.exists but if we are specifically looking for file we can use os.path.isfile.

Test whether a path is a symbolic link

>>> os.path.islink("/etc/localtime")
True

OS Walk

  • It’s used to generates the file names in a directory tree by walking the tree either top-down or bottom-up.
>>> import os
>>> os.walk("/home/prashant")
<generator object walk at 0x7f87f75545c8>

NOTE: It creates a generator object. To convert it into list

>>> list(os.walk("/home/prashant"))
[('/home/prashant', [], [])]
  • From a given path, Python is generating a list which contains tuples and each tuple consist of three values
  • First value is the path we have given “‘/home/prashant'”
  • Second Value is the list of directories in a given list
  • Third is list of files in a given directory

Let say in a given path, I will create some files and directories

 sudo tree
.
├── ashish
│   └── abhya
├── pankaj
│   └── newtestfile
└── test1

You will see the output like this

>>> list(os.walk("/home/prashant"))
[('/home/prashant', ['ashish', 'pankaj'], ['test1']), ('/home/prashant/ashish', [], ['abhya']), ('/home/prashant/pankaj', [], ['newtestfile'])]
  • If we are going to run the for loop on the top of it, we will get the same output
>>> for path in os.walk("/home/prashant"):
...     print(path)
... 
('/home/prashant', ['ashish', 'pankaj'], ['test1'])
('/home/prashant/ashish', [], ['abhya'])
('/home/prashant/pankaj', [], ['newtestfile'])

As you can see in the above output we are getting the tuple back. As we know we can unpack the tuple

>>> for rootpath, dirpath, filepath in os.walk("/home/prashant"):
...     print(rootpath)
... 
/home/prashant
/home/prashant/ashish
/home/prashant/pankaj

In the above example, I am only getting the root path back. If we want files in that path

>>> for rootpath, dirpath, filepath in os.walk("/home/prashant"):
...     print(rootpath, filepath)
... 
/home/prashant ['test1']
/home/prashant/ashish ['abhya']
/home/prashant/pankaj ['newtestfile']
  • The important scenario is where we want to join the top level directory with file
import os
path="/home/prashant"

for rootfile, dirname, filename in os.walk(path):
    for file in filename:
        print(os.path.join(rootfile, file))

and the output will be simply

$ python3 listfile.py 
/home/prashant/listfile.py
/home/prashant/test1
/home/prashant/ashish/abhya
/home/prashant/pankaj/newtestfile

We can also extend this concept to search for a particular file

import os
path="/etc"
file_search=input("Please enter the filename to search: ")
for rootfile, dirname, filename in os.walk(path):
    for file in filename:
        if file == file_search:  
            print(os.path.join(rootfile, file))

Please join me with my journey by following any of the below links