Open In App

Python Directory Management

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
  • filecmp
  • tempfile
  • shutil

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:

  • os.mkdir(name) method to create a new directory.
  • The desired name for the new directory is passed as the parameter.
  • By default it creates the new directory in the current working directory.
  • If the new directory has to be created somewhere else then that path has to be specified and the path should contain forward slashes instead of backward ones.

Python3




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


Getting Current Working Directory (CWD):

  • os.getcwd() can be used.
  • It returns a string that represents the path of the current working directory.
  • os.getcwdb() can also be used but it returns a byte string that represents the current working directory.
  • Both methods do not require any parameters to be passed.

Python3




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:

  • os.rename() method is used to rename the directory.
  • The parameters passed are old_name followed by new_name.
  • If a directory already exists with the new_name passed, OSError will be raised in case of both Unix and Windows.
  • If a file already exists with the new_name, in Unix no error arises, the directory will be renamed. But in Windows the renaming won’t happen and error will be raised.
  • os.renames(‘old_name’,’dest_dir:/new_name’) method works similar to os.rename() but it moves the renamed file to the specified destination directory(dest_dir).

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

Python3




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:

 

Python3




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


Changing Current Working Directory (CWD):

  • Every process in the computer system will have a directory associated with it, which is known as Current Working Directory(CWD).
  • os.chdir() method is used to change it.
  • The parameter passed is the path/name of the desired directory to which one wish to shift.

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

Python3




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

  • A directory may contain sub-directories and a number of files in it. To list them, os.listdir() method is used.
  • It either takes no parameter or one parameter.
  • If no parameter is passed, then the files and sub-directories of the CWD is listed.
  • If files of any other directory other than the CWD is required to be listed, then that directory’s name/path is passed as parameter.

 

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

 

Python3




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

  • os.rmdir() method is used to remove/delete a directory.
  • The parameter passed is the path to that directory.
  • It deletes the directory if and only if it is empty, otherwise raises an OSError.

 

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.

 

Python3




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:

  • Given a directory name or Path, os.path.isdir(path) is used to validate whether the path is a valid directory or not.
  • It returns boolean values only. Returns true if the given path is a valid directory otherwise false.

 

Python3




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:

  • os.path.getsize(path_name) gives the size of the directory in bytes.
  • OSError is raised if, invalid path is passed as parameter.

 

Python3




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


 
 

Output

4096

Getting access and modification times:

  • To get the last accessed time of a directory : os.path.getatime (path)
  • To get the last modified time of the directory : os.path.getmtime (path)
  • These methods return the number of seconds since the epoch. To format it, datetime module’s strftime( ) function can be used.

 

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

 

Python3




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.

Python3




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.

Python3




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

Python3




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:

  • This module is used to create temporary files and directories.
  • This creates the temporary files and directories in the temp directories created by the Operating systems.
  • For windows, the temp files can be found at the following path:

profile/AppData/Local/temp

mkdtemp() :

  • It is used to create a temporary directory by passing the parameters suffix, prefix and dir.
  • The suffix and prefix corresponds to the naming conventions for the created temp directory. suffix decides how the file name should end and prefix is decides the beginning of the name and mostly set to ‘tmp‘.
  • The dir parameter specifies the path where the temporary directory should be created. By default the dir is set to the temp directory created by the Operating systems.
  • This temporary directories are readable, writable and searchable only by the creator using the creator’s unique ID.
  • The user who created the temp dir is responsible for deleting the temporary directory upon completion of the work.
  • It returns the path of the created new directory.

Python3




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


Output

/tmp/tmp0ndvk7p_

TemporaryDirectory():

  • This function makes use of mkdtemp() to create a temporary directory and the same parameters are passed here also.
  • The main difference is, the object created as a result acts as a context manager.
  • The user need not manually delete the created temporary file, it is cleared automatically by the file system.
  • To get the name of the newly created temp directory, object.name can be used.
  • To delete the temporary dir explicitly, cleanup( ) function can be used.

Python3




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

  • Recursively copies the source directory (s) to the destination directory (d) provided that ‘d’ does not already exists.
  • symlinks are also known as symbolic links which denote some virtual files or reference folders or folders that located somewhere else. It can take values like true, false or omitted. If true, the symlinks in source are marked as symlinks in the destination also, but the associated metadata will not be copied. If the value is false or omitted, the contents as well as the metadata of the linked file are copied to the destination.
  • For ignore, a callable that takes the directory being visited and its contents like the return value of os.listdir() since, copytree() is a recursive functions. The files to be ignored while copying can be named using this parameter.
  • The copy2 function is used as the default copy_function because it allows copying metadata. copy(), copystat() can also be used.

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

  • Deletes an entire directory. This overcomes the main disadvantage of os.rmdir() that it deletes the directory only if it is empty.
  • The path should be a valid directory. The symlinks to a directory won’t be accepted.
  • If ignore_error is true, then the errors raised while removing the directory will be ignored. If false or omitted is given, then the raised errors should be handled by the ones mentioned in onerror parameter.
  • onerror is a callable that takes three parameters namely function, path and excinfo. The first is the function that raises the exception, the next is the path to be passed to the function and the last is the exception information returned by sys.exc_info().

shutil.move(s,d):

  • Recursively moves the source directory to the destination specified.
  • The destination is not supposed to exist already, if exists based on the os.rename() semantics, it will be overwritten.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads