Open In App

Python Directory Management

Directories are a way of storing, organizing, and separating the files on a computer. The directory that does not have a parent is called a root directory. The way to reach the file is called the path. The path contains a combination of directory names, folder names separated by slashes and colon and this gives the route to a file in the system.

Directory management using Python

Python contains several modules that has a number of built-in functions to manipulate and process data. Python has also provided modules that help us to interact with the operating system and the files. These kinds of modules can be used for directory management also. The modules that provide the functionalities are listed below:



os and os.path module

The os module is used to handle files and directories in various ways. It provides provisions to create/rename/delete directories. This allows even to know the current working directory and change it to another. It also allows one to copy files from one directory to another. The major methods used for directory management is explained below.

Creating new directory:




import os
 
# creates in current working directory
os.mkdir('new_dir'
 
# creates in D:\
os.mkdir('D:/new_dir'

Getting Current Working Directory (CWD):




import os
 
print("String format :", os.getcwd())
print("Byte string format :", os.getcwdb())

Output:



String format : /home/nikhil/Desktop/gfg
Byte string format : b'/home/nikhil/Desktop/gfg'

Renaming a directory:

For example, consider there is a file named ‘file1.txt’ in current working directory. Now to just rename it :




import os
 
os.rename('file1.txt','file1_renamed.txt')

 
 

If renaming and moving the file to some other directory is required, then the code snippet should be:

 




import os
 
os.renames('file1.txt', 'D:/file1_renamed.txt')

Changing Current Working Directory (CWD):

Form example, If we need to change the CWD to my_folder in D:/, then the following code snippet is used.




import os
 
print("Current directory :", os.getcwd())
 
# Changing directory
os.chdir('/home/nikhil/Desktop/')
print("Current directory :", os.getcwd())

 
 

Output:

 

Current directory : /home/nikhil/Desktop/gfg
Current directory : /home/nikhil/Desktop

Listing the files in a directory

 

For example: Listing the files in the CWD- GeeksforGeeks (root directory)

 




import os
 
print("The files in CWD are :",os.listdir(os.getcwd()))

 
 

Output:

 

The files in CWD are : [‘site folder’, ‘.directory’, ‘proxy.txt’, ‘poem python.txt’, ‘left bar’, ‘images’, ‘Welcome to GeeksforGeeks!\nPosts Add Ne.txt’, ‘trash.desktop’, ‘gfg.py’, ‘Sorry, you can not update core for some tim.txt’, ‘gfgNikhil Aggarwal.png’, ‘0001.jpg’, ‘gfg’, ‘gfgcerti.png’, ‘raju’, ‘images big’]

Removing a directory

 

For example, Let us consider a directory K:/files. Now to remove it, one has to ensure whether it is empty and then proceed for deleting.

 




import os
 
dir_li=os.listdir('k:/files')
 
if len(dir_li)==0:
  print("Error!! Directory not empty!!")
else:
  os.rmdir('k:/files')

 
 

To check whether it is a directory:

 




import os
 
# current working directory of
# GeeksforGeeks
cwd='/'
print(os.path.isdir(cwd))
 
# Some other directory
other='K:/'
print(os.path.isdir(other))

 
 

Output
True
False

To get size of the directory:

 




import os
 
print(os.path.getsize(os.getcwd()))

 
 

Output
4096

Getting access and modification times:

 

Example : Getting access and modification time of GeeksforGeeks (root) directory

 




import os
import datetime as dt
 
print("Before conversion :")
 
# returns seconds since epoch
print("last access time :",os.path.getatime(os.getcwd()))
print("last modification time :",os.path.getmtime(os.getcwd()))
 
print("After conversion :")
 
# formatting the return value
access_time=dt.datetime.fromtimestamp(os.path.getatime(os.getcwd())).strftime('%Y-%m-%d %I:%M %p')
modification_time=dt.datetime.fromtimestamp(os.path.getmtime(os.getcwd())).strftime('%Y-%m-%d %I:%M %p')
 
print("last access time :",access_time)
print("last modification time :",modification_time)

Output
Before conversion :
last access time : 1596897099.56
last modification time : 1596897099.56
After conversion :
last access time : 2020-08-08 02:31 PM
last modification time : 2020-08-08 02:31 PM

 

filecmp module

This module provides various functions to perform comparison between files and directories. To compare the directories, an object has to be created for the class filecmp.dircmp that describes which files to ignore and which files to hide from the functions of this class. The constructor has to be invoked before calling any of the functions of this class. The constructor can be invoked as given below:

d = filecmp . dircmp( dir_1, dir_2, ignore=[a,b,c]/None, hide=[d,e,f]/None )

Functions for comparing directories:

  1. d.report() : Compares the two directories given while invoking the constructor and presents a summary regarding the list of files in both the directories. In case any identical files are found, they are also listed. Common sub-directories are also printed in the output.




import filecmp as fc
import os
 
dir_1 = dir_2 = os.getcwd()
 
# creating object and invoking constructor
d = fc.dircmp(dir_1, dir_2, ignore=None, hide=None)
 
print("comparison 1 :")
d.report()

Output
comparison 1 :
diff / /
Common subdirectories : ['bin', 'boot', 'dev', 'etc', 'home', 'lib', 'lib64', 'media', 'mnt', 'opt', 'proc', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var']

       2. d.report_partial_closure() :  Prints out the comparison between the directories passed and also the comparisons between the immediate common               sub-directories.




import filecmp as fc
import os
 
dir_1 = dir_2 = os.getcwd()
 
# creating object and invoking constructor
d = fc.dircmp(dir_1, dir_2, ignore=None, hide=None)
 
print("comparison 2 :")
d.report_partial_closure()

Output
comparison 2 :
diff / /
Common subdirectories : ['bin', 'boot', 'dev', 'etc', 'home', 'lib', 'lib64', 'media', 'mnt', 'opt', 'proc', 'run', 'sbin', 'srv', 'sys', 'tmp', 'usr', 'var']

diff /bin /bin
Identical files : ['bash', 'btrfs', 'btrfs-calc-size', 'btrfs-convert', 'btrfs-debug-tree', 'btrfs-find-root', 'btrfs-image', 'btrfs-map-logical', 'btrfs-select-super', 'btrfs-show-super', 'btrfs-zero-log', 'btrfsck', 'btrfstune', 'bunzip2', 'busybox', 'bzcat', 'bzcmp', 'bzdiff', 'bzegrep', 'bzexe', 'bzfgrep', 'bzgrep', 'bzip2', 'bzip2recover', 'bzless', 'bzmore', 'cat', 'chacl', 'chgrp', 'chmod', 'chown', 'chvt', 'cp', 'cpio', 'dash', 'date', 'dd', 'df', 'dir', 'dmesg', 'dnsdomainname', 'domainname', 'dumpkeys', 'echo', 'ed', 'egrep', 'false', 'fgconsole', 'fgrep', 'findmnt', 'fsck.btrfs', 'fuser', 'fusermount', 'getfacl', 'grep', 'gunzip', 'gzexe', 'gzip', 'hostname', 'ip', 'journalctl', 'kbd_mode', 'kill', 'kmod', 'less', 'lessecho', 'lessfile', 'lesskey', 'lesspipe', 'ln', 'loadkeys', 'login', 'loginctl', 'lowntfs-3g', 'ls', 'lsblk', 'lsmod', 'mkdir', 'mkfs.btrfs', 'mknod', 'mktemp', 'more', 'mount', 'mountpoint', 'mt', 'mt-gnu', 'mv', 'nano', 'nc', 'nc.openbsd', 'netcat', 'netstat', 'networkctl', 'nisdomainname', 'ntfs-3g', 'ntfs-3g.probe', 'ntfs-3g.secaudit', 'ntfs-3g.usermap', 'ntfscat', 'ntfscluster', 'ntfscmp', 'ntfsfallocate', 'ntfsfix', 'ntfsinfo', 'ntfsls', 'ntfsmove', 'ntfstruncate', 'ntfswipe', 'open', 'openvt', 'pidof', 'ping', 'ping6', 'plymouth', 'ps', 'pwd', 'rbash', 'readlink', 'red', 'rm', 'rmdir', 'rnano', 'run-parts', 'sed', 'setfacl', 'setfont', 'setupcon', 'sh', 'sh.distrib', 'sleep', 'ss', 'static-sh', 'stty', 'su', 'sync', 'systemctl', 'systemd', 'systemd-ask-password', 'systemd-escape', 'systemd-hwdb', 'systemd-inhibit', 'systemd-machine-id-setup', 'systemd-notify', 'systemd-tmpfiles', 'systemd-tty-ask-password-agent', 'tailf', 'tar', 'tempfile', 'touch', 'true', 'udevadm', 'ulockmgr_server', 'umount', 'uname', 'uncompress', 'unicode_start', 'vdir', 'wdctl', 'which', 'whiptail', 'ypdomainname', 'zcat', 'zcmp', 'zdiff', 'zegrep', 'zfgrep', 'zforce', 'zgrep', 'zless', 'zmore', 'znew']

diff /boot /boot
Common subdirectories : ['grub']

diff /dev /dev
Identical files : ['console', 'core', 'full', 'fuse', 'null', 'ptmx', 'random', 'stderr', 'stdout', 'tty', 'urandom', 'zero']
Common subdirectories : ['.lxd-mounts', 'fd', 'hugepages', 'lxd', 'mqueue', 'net', 'pts', 'shm']
Common funny cases : ['initctl', 'log', 'stdin']

diff /etc /etc
Identical files : ['.pwd.lock', 'adduser.conf', 'at.deny', 'bash.bashrc', 'bash_completion', 'bindresvport.blacklist', 'ca-certificates.conf', 'ca-certificates.conf.dpkg-old', 'crontab', 'crypttab', 'debconf.conf', 'debian_version', 'deluser.conf', 'ec2_version', 'environment', 'fstab', 'fuse.conf', 'gai.conf', 'group', 'group-', 'gshadow', 'gshadow-', 'hdparm.conf', 'host.conf', 'hostname', 'hosts', 'hosts.allow', 'hosts.deny', 'inputrc', 'insserv.conf', 'issue', 'issue.net', 'kernel-img.conf', 'ld.so.cache', 'ld.so.conf', 'legal', 'libaudit.conf', 'locale.alias', 'locale.gen', 'localtime', 'login.defs', 'logrotate.conf', 'lsb-release', 'ltrace.conf', 'machine-id', 'magic', 'magic.mime', 'mailcap', 'mailcap.order', 'manpath.config', 'matplotlibrc', 'mime.types', 'mke2fs.conf', 'modules', 'mtab', 'nanorc', 'networks', 'nsswitch.conf', 'os-release', 'overlayroot.conf', 'overlayroot.local.conf', 'pam.conf', 'passwd', 'passwd-', 'popularity-contest.conf', 'profile', 'protocols', 'rc.local', 'resolv.conf', 'rmt', 'rpc', 'rsyslog.conf', 'screenrc', 'securetty', 'services', 'shadow', 'shadow-', 'shells', 'sos.conf', 'subgid', 'subgid-', 'subuid', 'subuid-', 'sudoers', 'sysctl.conf', 'timezone', 'ucf.conf', 'updatedb.conf', 'vtrgb', 'wgetrc', 'zsh_command_not_found']
Common subdirectories : ['NetworkManager', 'X11', 'acpi', 'alternatives', 'apache2', 'apm', 'apparmor', 'apparmor.d', 'apport', 'apt', 'bash_completion.d', 'binfmt.d', 'byobu', 'ca-certificates', 'calendar', 'cloud', 'console-setup', 'cron.d', 'cron.daily', 'cron.hourly', 'cron.monthly', 'cron.weekly', 'dbus-1', 'default', 'depmod.d', 'dhcp', 'dpkg', 'emacs', 'fonts', 'groff', 'gss', 'init', 'init.d', 'initramfs-tools', 'insserv', 'insserv.conf.d', 'iproute2', 'iscsi', 'kbd', 'kernel', 'ld.so.conf.d', 'ldap', 'lighttpd', 'logcheck', 'logrotate.d', 'lvm', 'mdadm', 'modprobe.d', 'modules-load.d', 'network', 'newt', 'opt', 'pam.d', 'perl', 'php', 'pm', 'polkit-1', 'pollinate', 'ppp', 'profile.d', 'python', 'python2.7', 'python3', 'python3.5', 'python3.6', 'rc0.d', 'rc1.d', 'rc2.d', 'rc3.d', 'rc4.d', 'rc5.d', 'rc6.d', 'rcS.d', 'resolvconf', 'rsyslog.d', 'security', 'selinux', 'sgml', 'skel', 'ssh', 'ssl', 'sudoers.d', 'sysctl.d', 'systemd', 'terminfo', 'tmpfiles.d', 'udev', 'ufw', 'update-manager', 'update-motd.d', 'update-notifier', 'vim', 'vmware-tools', 'xdg', 'xml']

diff /home /home
Identical files : ['e94ae880c0dbde73291e3fa78cd4c133.py']

diff /lib /lib
Identical files : ['cpp', 'klibc-k3La8MUnuzHQ0_kG8hokcGAC0PA.so', 'libhandle.so.1', 'libhandle.so.1.0.3']
Common subdirectories : ['apparmor', 'cryptsetup', 'hdparm', 'ifupdown', 'init', 'lsb', 'modprobe.d', 'modules', 'modules-load.d', 'open-iscsi', 'recovery-mode', 'resolvconf', 'systemd', 'terminfo', 'udev', 'ufw', 'x86_64-linux-gnu', 'xtables']

diff /lib64 /lib64
Identical files : ['ld-linux-x86-64.so.2']

diff /media /media

diff /mnt /mnt

diff /opt /opt

diff /proc /proc
Identical files : ['buddyinfo', 'cgroups', 'cmdline', 'consoles', 'cpuinfo', 'crypto', 'devices', 'diskstats', 'dma', 'execdomains', 'fb', 'filesystems', 'interrupts', 'iomem', 'ioports', 'kallsyms', 'kcore', 'key-users', 'keys', 'kmsg', 'kpagecgroup', 'kpagecount', 'kpageflags', 'loadavg', 'locks', 'mdstat', 'meminfo', 'misc', 'modules', 'mounts', 'mtrr', 'pagetypeinfo', 'partitions', 'sched_debug', 'schedstat', 'slabinfo', 'softirqs', 'stat', 'swaps', 'sysrq-trigger', 'timer_list', 'timer_stats', 'uptime', 'version', 'version_signature', 'vmallocinfo', 'vmstat', 'zoneinfo']
Common subdirectories : ['1', '265', '29568', '29569', '29570', '348', '350', '354', '359', '363', '364', '374', '395', '413', '440', '441', '442', '55', '58', 'acpi', 'bus', 'driver', 'fs', 'irq', 'net', 'scsi', 'self', 'sys', 'sysvipc', 'thread-self', 'tty']

diff /run /run
Identical files : ['agetty.reload', 'atd.pid', 'crond.pid', 'crond.reboot', 'dhclient.eth0.pid', 'mlocate.daily.lock', 'rsyslogd.pid', 'sshd.pid', 'utmp']
Common subdirectories : ['cloud-init', 'dbus', 'lock', 'log', 'lvm', 'mount', 'network', 'php', 'resolvconf', 'screen', 'sendsigs.omit.d', 'shm', 'sshd', 'sudo', 'systemd', 'udev', 'user', 'uuidd']
Common funny cases : ['acpid.socket', 'apport.socket', 'dmeventd-client', 'dmeventd-server', 'initctl', 'snapd-snap.socket', 'snapd.socket']

diff /sbin /sbin
Identical files : ['MAKEDEV', 'acpi_available', 'agetty', 'apm_available', 'apparmor_parser', 'badblocks', 'blkdiscard', 'blkid', 'blockdev', 'bridge', 'capsh', 'cfdisk', 'cgdisk', 'chcpu', 'cryptdisks_start', 'cryptdisks_stop', 'cryptsetup', 'cryptsetup-reencrypt', 'ctrlaltdel', 'debugfs', 'depmod', 'dhclient', 'dhclient-script', 'dmeventd', 'dmsetup', 'dosfsck', 'dosfslabel', 'dumpe2fs', 'e2fsck', 'e2image', 'e2label', 'e2undo', 'ethtool', 'fatlabel', 'fdisk', 'findfs', 'fixparts', 'fsadm', 'fsck', 'fsck.cramfs', 'fsck.ext2', 'fsck.ext3', 'fsck.ext4', 'fsck.ext4dev', 'fsck.fat', 'fsck.minix', 'fsck.msdos', 'fsck.nfs', 'fsck.vfat', 'fsck.xfs', 'fsfreeze', 'fstab-decode', 'fstrim', 'gdisk', 'getcap', 'getpcaps', 'getty', 'halt', 'hdparm', 'hwclock', 'ifconfig', 'ifdown', 'ifenslave', 'ifenslave-2.6', 'ifquery', 'ifup', 'init', 'insmod', 'installkernel', 'ip', 'ip6tables', 'ip6tables-restore', 'ip6tables-save', 'ipmaddr', 'iptables', 'iptables-restore', 'iptables-save', 'iptunnel', 'iscsi-iname', 'iscsi_discovery', 'iscsiadm', 'iscsid', 'iscsistart', 'isosize', 'kbdrate', 'killall5', 'ldconfig', 'ldconfig.real', 'logsave', 'losetup', 'lsmod', 'lvchange', 'lvconvert', 'lvcreate', 'lvdisplay', 'lvextend', 'lvm', 'lvmchange', 'lvmconf', 'lvmconfig', 'lvmdiskscan', 'lvmdump', 'lvmetad', 'lvmpolld', 'lvmsadc', 'lvmsar', 'lvreduce', 'lvremove', 'lvrename', 'lvresize', 'lvs', 'lvscan', 'mdadm', 'mdmon', 'mii-tool', 'mkdosfs', 'mke2fs', 'mkfs', 'mkfs.bfs', 'mkfs.cramfs', 'mkfs.ext2', 'mkfs.ext3', 'mkfs.ext4', 'mkfs.ext4dev', 'mkfs.fat', 'mkfs.minix', 'mkfs.msdos', 'mkfs.ntfs', 'mkfs.vfat', 'mkfs.xfs', 'mkhomedir_helper', 'mkntfs', 'mkswap', 'modinfo', 'modprobe', 'mount.fuse', 'mount.lowntfs-3g', 'mount.ntfs', 'mount.ntfs-3g', 'mount.vmhgfs', 'nameif', 'ntfsclone', 'ntfscp', 'ntfslabel', 'ntfsresize', 'ntfsundelete', 'on_ac_power', 'pam_extrausers_chkpwd', 'pam_extrausers_update', 'pam_tally', 'pam_tally2', 'parted', 'partprobe', 'pivot_root', 'plipconfig', 'plymouthd', 'poweroff', 'pvchange', 'pvck', 'pvcreate', 'pvdisplay', 'pvmove', 'pvremove', 'pvresize', 'pvs', 'pvscan', 'rarp', 'raw', 'reboot', 'resize2fs', 'resolvconf', 'rmmod', 'route', 'rtacct', 'rtmon', 'runlevel', 'runuser', 'setcap', 'setvtrgb', 'sfdisk', 'sgdisk', 'shadowconfig', 'shutdown', 'slattach', 'start-stop-daemon', 'sulogin', 'swaplabel', 'swapoff', 'swapon', 'switch_root', 'sysctl', 'tc', 'telinit', 'tipc', 'tune2fs', 'udevadm', 'unix_chkpwd', 'unix_update', 'ureadahead', 'vconfig', 'veritysetup', 'vgcfgbackup', 'vgcfgrestore', 'vgchange', 'vgck', 'vgconvert', 'vgcreate', 'vgdisplay', 'vgexport', 'vgextend', 'vgimport', 'vgimportclone', 'vgmerge', 'vgmknodes', 'vgreduce', 'vgremove', 'vgrename', 'vgs', 'vgscan', 'vgsplit', 'wipefs', 'xfs_repair', 'xtables-multi', 'zramctl']

diff /srv /srv

diff /sys /sys
Common subdirectories : ['block', 'bus', 'class', 'dev', 'devices', 'firmware', 'fs', 'hypervisor', 'kernel', 'module', 'power']

diff /tmp /tmp
Common subdirectories : ['.ICE-unix', '.Test-unix', '.X11-unix', '.XIM-unix', '.font-unix']

diff /usr /usr
Common subdirectories : ['bin', 'games', 'include', 'lib', 'local', 'sbin', 'share', 'src']

diff /var /var
Common subdirectories : ['backups', 'cache', 'crash', 'lib', 'local', 'lock', 'log', 'mail', 'opt', 'run', 'snap', 'spool', 'tmp']

        3. d.report_full_closure() : It is same as the previous one, but does the work recursively.  It compares and displays the common sub-directories,                     identical files and common funny cases (the two things do not match in type, ie., one is a directory and another is a file.)




import filecmp as fc
import os
 
dir_1 = dir_2 = os.getcwd()
 
# creating object and invoking constructor
d = fc.dircmp(dir_1, dir_2, ignore=None, hide=None)
 
print("comparison 3 :")
d.report_full_closure()

tempfile module:

profile/AppData/Local/temp

mkdtemp() :




import tempfile as tf
 
f = tf.mkdtemp(suffix='', prefix='tmp')
print(f)

Output
/tmp/tmp0ndvk7p_

TemporaryDirectory():




import tempfile as tf
 
f = tf.TemporaryDirectory(suffix='', prefix='tmp')
print("Temporary file :", f.name)
 
f.cleanup()

Output
Temporary file : /tmp/tmpp3wr65fj

shutil module

This module is concerned with number of high-level operations on files and directories. It allows copying/moving directories from a source to destination.

shutil.copytree(s, d, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False):

shutil.rmtree(path, ignore_errors=False, onerror=None):

shutil.move(s,d):


Article Tags :