From 69cfa85a7539ec0a67ebb1226c5ba747a8c18d8a Mon Sep 17 00:00:00 2001
From: Harsh <39079018+ksharshveer@users.noreply.github.com>
Date: Fri, 22 Jan 2021 14:43:07 -0800
Subject: [PATCH] Changes in section "Python OS" (#124)
* Added answer in Python OS
---
README.md | 32 ++++++++++++++++++++++++++++----
1 file changed, 28 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 7ad1b5f..9ece8cf 100644
--- a/README.md
+++ b/README.md
@@ -6297,18 +6297,42 @@ with open('file.txt', 'w') as file:
How to print current working directory?
+
+ import os
+
+ print(os.getcwd())
+
Given the path /dir1/dir2/file1
print the file name (file1)
+
+ import os
+
+ print(os.path.basename('/dir1/dir2/file1'))
+
+ # Another way
+ print(os.path.split('/dir1/dir2/file1')[1])
+
-Given the path /dir1/dir2/file1
print the name of the directory where the file resides (dir2)
-
+Given the path /dir1/dir2/file1
+
+1. Print the path without the file name (/dir1/dir2)
+2. Print the name of the directory where the file resides (dir2)
+
+
+ import os
+
+ ## Part 1.
+ # os.path.dirname gives path removing the end component
+ dirpath = os.path.dirname('/dir1/dir2/file1')
+ print(dirpath)
+
+ ## Part 2.
+ print(os.path.basename(dirpath))
-
-Given the path /dir1/dir2/file1
print the path without the file name (/dir1/dir2)