python os template manipulating files and directories common methods

  • python common operations files and directories methods:
  1. Python script to get the current working directory path: os.getcwd ()
  2. All files and directories return under the specified directory: os.listdir (). For example: Returns the file under the C drive: os.listdir ( "C: \")
  3. To delete a file: os.remove (filepath)
  4. To delete multiple empty directory: os.removedirs (r'd: python ')
  5. Whether given path is a file: os.path.isfile (filepath)
  6. Whether given path is a directory: os.path.isdir (filepath)
  7. Determine whether the absolute path: os.isabs ()
  8. Verify that the path really exists: os.path.exists (). Such as whether there is Python folder Inspection D drive: os.path.exists (r'd: \ python ')
  9. A separate path of the directory name and file name: os.path.split (). For example: os.path.split (r '/ home / qiye / qiye.txt)

    returns a result tuples: (' / home / qiye ' ,' qiye.txt ')
  10. Extension separation: os.path.splitext (). For example: For example: os.path.split (r '/ home / qiye / qiye.txt)

    returns a result tuples: (' / home / qiye / qiye ',' txt '.)
  11. Get the path name: os.path.dirname (filepath)
  12. Get File name: os.path.basename (filepath)
  13. Read and set environment variables: os.getenv () and os.putenv ()
  14. Rename the file or directory: os.rename (old, new)
  15. Create multi-level directory: os.makedirs (r'c: \ python \ test ')
  16. Create a single directory: os.mkdir ( "test")
  17. Getting file attributes: os.stat (file)
  18. Get File Size: os.getsize (filename)
  19. Delete the directory: os.rmdir ( "dir") can only delete empty directories
  20. Get the file attributes and time stamp: os.chmod (file)

Guess you like

Origin www.cnblogs.com/pengjie-py/p/11261111.html