Details of Python os operation method of

  • 2020-06-03 07:12:25
  • OfStack

os. path. driname(path) : Returns the path string one level above the path.


  >>> os.path.dirname('D:\Games')
  'D:\\'
  >>>

os. path. basename(path) : Returns the last level of directory name (folder name) or file name (full name) of the path.


  >>> os.path.basename('D:\Games\9yin_632\ The snail the whole package \\0x0804.ini')
  '0x0804.ini'
  >>>

os.path.splitext (file_name) : Returns the tuple of the filename and its suffix.


  >>> os.path.splitext('0x0804.ini')
  ('0x0804', '.ini')
  >>>

os.path.abspath (string) : Returns the path of the current working directory plus the path string of string.


  >>> os.path.abspath('Games') #  There is no" Games "This file or folder is just a random string 
  'C:\\Python27\\Games'
  >>>

os. path. isdir(path) : Determine if a path is a directory (folder).

6. os. path. isfile(path) : Determine if a path is a file.

os.listdir (dir_path) : Returns all files (full name) and folder names under 1 directory (dir_path can only be a directory, not a filename path) as a list.

8. os. remove(file_path) : Deletes the specified file.

9. os. removedirs(dir_path) : Removes the specified empty directory (empty folder).

10. os.path.exists(path) : Judge whether a path exists.

11. os.mkdir(path) : Create a new directory (folder).

12. os.getcwd() : Gets the current working directory.


Related articles: