DIRECTORY MANIPULATION IN PYTHON
Python directory
In this session, we will see how we can manipulate our directories in python with help of built-in function
How we can go to the current working directory…?
There is a function like getcwd() with help of this we can get our current working directory with help of os module we need to import while running the program
Example :
Import os
Cwd1 = os . getcwd()
Print (cwd)
Now if you want to change the directory we can use chdir() function see how it will work :
Import os
Os. Chdir (‘ c : / / temp123456 ‘)
Cwd 2 = os . getcwd ()
Print (cwd2)
For Free, Demo classes Call: 02071171500
Registration Link: Click Here!
What is meant by join and split path…?
Normally the join() function is used for path components and it gives return path with help of separator platform instead of in unix / in windows backslash.
And if we think about split() function it divides the path to separate without using separator path
Following example show how join() and split path work…?
Import os
Fp1 = os . path . join (‘temp1’ , ‘python3’)
Print (fp1) # temp1 \ python3 ( in windows os)
Pc = os . path . split (fp)
Print (pc) # (‘temp’ , ‘python’)
If you test path is a directory or not ……?
If we need to chech that path is existed or not then there is functions like exists() and isdir() in module os . path as given below :
Import os
Path1 = os . path . join (‘ c ’ : \ \ , ‘ temp ’)
If os . path . exists (path1) :
Print (path1 + ‘ : exists’)
If os . path . isdir (path1) :
Print (path1 + ‘ : is a directory which is not existed’)
How to create a directory ……..?
If you want to make a new directory we have a built in function for that known as mkdir () or makedirs () functions which are already present in os module. And don’t forget to check that the directory which you making is already existed or not.
In the following example we are making a new directory :
Import os
Dir1 = os . path . join (‘c : \ \ ’, ‘temp123’ , ‘python321’)
If not os . path . exists (dir) :
Os . mkdir (dir)
We can rename the directory also how ….?
For renaming a directory we have a built-in function named as os . rename() where we need to pass the directory which we want to change and add a new path directory name. SevenMentor Python training in Pune provides a full-time training opportunity to learn the various aspects of Python
For example :
Old path1 = os . path . join (‘c : \\’, ‘temp’ , ‘python’ )
New path1 = os . path . join (‘c : \\ ’, ‘temp’ , ‘python3’)
For Free, Demo classes Call: 02071171500
Registration Link: Click Here!
If os . path . exists (old path1):
Os . rename (old path1, new path1)
Print (‘’{0}’ is renamed to be ‘{1}’’ . format (old pathq1 , new path1))
So the output will come like
‘c : \ temp \ python ’ is renamed to be ‘c :\ temp \ python3’
For deleting the directory what is the procedure …..?
To delete any directory there is again one in-built function available in our platform known as rmdir()
How does it work? let’s see
Import os
Dir1 = os . path . join (‘c : \ \’ , ‘temp’ , ‘python’)
If os . path . exists (dir1):
Os . rmdir(dir1)
Print (dir1 + ‘is removed’)
If you want to trace any directory for that …?
To traverse a directory we have in built function named as os . walk () function which gives us permission to traverse the directory , the sub directories and files also .
For Free, Demo classes Call: 02071171500
Registration Link: Click Here!
Following example will display how we are tracing the directory with help of that function :
Import os
Root dir1 = ‘c : \\ temp’
For root, dirs, files, in os . walk (root dir1):
Print (‘{0} has {1} files’. Format (root , len (files)))
So here if I conclude the data like
We learn following things
How to check current working directory ?
How to change the directory ?
How to rename directory ?
How to traverse directory ?
Author:-
Call now!!! | SevenMentor Pvt Ltd.
© Copyright 2021 | Sevenmentor Pvt Ltd.