Python Directory
A directory or folder is a collection of files and subdirectories.
Python has the os module that provides us with many useful
methods to work with directories and files.
Open():
The python open() function is used to open() internally stored files. It
returns the contents of the file as python objects.
Syntax: open (file_name, mode)
exists()
[Link]() method in Python is used to check whether the
specified path exists or not. This method can be also used to check
whether the given path refers to an open file descriptor or not.
import os >>>
path = 'C:\python37' True
a = [Link](path) >>>
print(a)
[Link]() method in Python is used to check whether the specified
path is an existing regular file or not.
import os >>>
path = 'C:/Users/systems/Desktop/[Link]'
a= [Link](path) True
print(a)
>>>
isdir()
[Link]() method in Python is used to check whether the
specified path is an existing directory or not.
Syntax: [Link](path)
[Link]() method in Python is used to create a hard link. This
method creates a hard link pointing to
the source named destination.
import os
src = 'C:/Users/systems/Desktop/[Link]'
dst =
'C:/Users/systems/Desktop/file(link).txt'
[Link](src, dst)
print("Hard link created successfully")
>>>
========================= RESTART: C:\python37\[Link] =========================
Hard link created successfully
>>>
Ex: import os
Get Current Directory
getcwd() : We can get the present working directory
>>> import os
>>> print([Link]())
C:\python37
getcwdb(): We can use the method to get it as bytes object.
>>> import os
>>> print([Link]())
b'C:\\python37'
listdir(): All files and sub-directories inside a directory can
be retrieved using the listdir() method.
>>> [Link]()
['[Link]', 'DLLs', 'Doc', 'include', 'Lib', 'libs', '[Link]', 'mysql-connector-
[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', 'Scripts', '[Link]',
'[Link]', 'tcl', 'Tools', '[Link]', '[Link]']
mkdir() : We can make a new directory using the mkdir() method.
The new directory is created in the current working directory.
>>> [Link]('msccs')
>>> [Link]()
['DLLs', 'Doc', 'include', 'Lib', 'libs', '[Link]', 'msccs', 'mysql-connector-python-
[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', 'Scripts', 'tcl', 'Tools',
'[Link]']
rename(): The rename() method can rename a directory or a file.
>>> [Link]('msccs','msccs2021')
>>> [Link]()
['DLLs', 'Doc', 'include', 'Lib', 'libs', '[Link]', 'msccs2021', 'mysql-connector-
[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', 'Scripts', '[Link]', 'tcl',
'Tools', '[Link]']
remove() rmdir() : A file can be removed (deleted) using the remove()
method. Similarly, the rmdir() method removes an empty directory.
>>> [Link]('[Link]')
>>> [Link]('msccs2021')
Shutil:
Shutil module offers high-level operation on a file like a copy, create,
and remote operation on the file. It comes under Python’s standard
utility modules. This module helps in automating the process of
copying and removal of files and directories.
Copying Files to another directory
[Link]() method in Python is used to copy the content of the source file to the
destination file or directory.
import shutil
source = "c:/python37/[Link]"
destination ="c:/python37/[Link]"
dest = [Link](source, destination)
print("Destination path:", dest)
Removing a Directory
[Link]() is used to delete an entire directory tree, the path must
point to a directory (but not a symbolic link to a directory).
import shutil
import os
location = "c:\python37"
dir = "priya"
path = [Link](location, dir)
[Link](path)