Handling Current Working Directory
We can consider Current Working Directory (CWD ) as a folder, Whenever the files are called only by their name, Python operating system (here python work as an operating system) assumes that is starts in the CWD which means that name-only reference will be successful only if the file is in the python's CWD.
Note:-
The folder where the python script is running is known as the Current Directory.
To get the location of the current working directory os.getcwd() is used.
Example:- C:UsershpOneDriveDocumentsDesktopd
For changing the current working directory(CWD) os.chdir() function is used.This function change the CWD to a specified path. It only takes a one argument as a new directory path.
Output:-
Current Working Directory: C:\Users\hp\OneDrive\Documents\Desktop\ os.listdir() function is used to get the list of all files and directoies in the specified directory. If we don't specify any directory, then the list of files and directories in the current working directory will be returned.
Example:-
Output:-
4. Creating a Directory
['25thMay2021.docx','com.moodle.moodlemobile','Demo','Desktop',
'desktop.ini']
Output:-
['.ipynb_checkpoints','.python.py.swp', 'Adobe Reader XI.lnk', 'bank.db','data','hurdle_task.py','log.txt','module1','Pandas_Exercises.ipynb',
'Python file', 'Untitled2.ipynb']
directory module2.
os module proves different methods for removing directories and files in Python. These are
Example:
['25th May 2021.docx', 'com.moodle.moodlemobile', 'Desktop', 'desktop.ini']
os.removedirs() function is used to remove or delete nested directory.
Example:
['.ipynb_checkpoints',
'.python.py.swp', 'Adobe Reader XI.lnk', 'bank.db', 'data',
'hurdle_task.py',log.txt','Pandas_Exercises.ipynb', 'Python file',
'Untitled2.ipynb']
Comments