diff --git a/README.md b/README.md index 39e12ee..81520fe 100644 --- a/README.md +++ b/README.md @@ -6337,18 +6337,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)