Os.path usage analysis in Python

  • 2020-04-02 14:28:35
  • OfStack

This article provides an example of the os.path usage in Python. Share with you for your reference. The details are as follows:

#coding=utf-8
import os
print os.path.abspath("d:\new\test.txt")
print os.path.basename("d:\new\test.txt")
print os.path.dirname("d:\new\test.txt")
print os.path.exists("d:\new")
print os.path.lexists("d:\new")
print os.path.expanduser("d:\new\text.txt")
print os.path.getatime("d:\new")  # Last access time
print os.path.getmtime("d:\new") # Last modified path time
print os.path.getctime("d:\new")  # Creation time
print os.path.getsize("d:\new\")  # Maybe the size of the path Bytes in units
print os.path.isabs("d:\")  # Is it an absolute path
print os.path.isfile("d:\new\hello.txt")
print os.path.isdir("d:\new")
print os.path.islink("d:\new\hello.txt")
print os.path.join("d:\new","hello.txt")
print os.path.normcase("d:\new\hello.txt")
print os.path.relpath("d:\new\hello.txt")  # Relative paths
print os.path.split("d:\new\hello.txt")  # Split file name
print os.path.splitdrive("d:\new\hello.txt")  # Detached disk drive
print os.path.splitext("d:\new\hello.txt")  # Detached extension


Operation results:
> > >
D: \ new \ test. TXT
Test the. TXT
D: \ new
True,
True,
D: \ new \ text. TXT
1322235096.47
1322235096.47
1321610018.9
16384
True,
True,
True,
False
D: \ new \ hello. TXT
D: \ new \ hello. TXT
Hello. TXT
(' d: \ \ new ', 'hello. TXT)
(' d: ', '\ \ new \ \ hello TXT)
(' d: \ \ new \ \ hello ', '. TXT ')
> > >

I hope this article has helped you with your Python programming.


Related articles: