Welcome

sampleqa.in tutorials, Python Programming  tutorial

Python OS.PATH module examples


methodDescExample
os.path.curdiralways returns .
       	>>>os.path.curdir
       	.
       	>>>os.path.realpath(os.path.curdir)
       	/home/jhonpeter
       	
       	realpath method expands . to absolute path in Linux
       
os.path.sepfile path seperator for Linux forward slash /
for Windows backward slash \
>>>os.path.sep
/ #for linux \ #for Windows
os.path.basenamegets filename for a given path
>>>os.path.basename('/var/log/log220902.log')
       'log220902.log'
       
os.path.getsizesize of the fileos.path.getsize('/home/jhonpeter/Documents/docs/spring-boot-actuator-web-api.pdf')
Displays size in bytes
os.path.isdir
os.path.isfile
check for given path is a directory or file
       	>>>os.path.isdir('/var/ftp')
       	True
 	>>> os.path.isfile('/var/log/boot.log')
	True
     	--both file and directory should exists in the system
      
os.path.getatime
os.path.getctime
os.path.getmtime
Every file in the system has last accessed time, last modified time, last changed time
>>> os.path.getatime('/var/log/boot.log')
1664476200.7197595
>>> os.path.getctime('/var/log/boot.log')
1664504540.176316
>>> os.path.getmtime('/var/log/boot.log')
1664504540.176316
>>> 
       
os.path.expandvarsExpand Linux Shell Variables in Python,
similarly in Windows Environment Variables
       >>>os.path.expandvars('$HOME')
       /home/jhonpeter
       # here $HOME is a Shell Environment variable.
       
os.path.joinjoin method is used to build a path from multiple variables.
       >>> os.path.join('/var','log','tmp.log')
	'/var/log/tmp.log'
	
	# it builds a path 
       

ADS