Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

os.path.dirname(__file__)

# Python program to explain os.path.dirname() method 
    
# importing os.path module 
import os.path
  
# Path
path = '/home/User/Documents'
  
# Get the directory name  
# from the specified path
dirname = os.path.dirname(path)
  
# Print the directory name  
print(dirname)
  
  
# Path
path = '/home/User/Documents/file.txt'
  
# Get the directory name  
# from the specified path
dirname = os.path.dirname(path)
  
# Print the directory name  
print(dirname)
  
  
# Path
path = 'file.txt'
  
# Get the directory name  
# from the specified path
dirname = os.path.dirname(path)
  
# Print the directory name  
print(dirname)
  
# In the above specified path 
# does not contains any
# directory so, 
# It will print Nothing
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
8+3 =