SoFunction
Updated on 2025-03-01

Python's Os operation method (detailed explanation)

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

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

3.os.path.isdir(path): determines whether a path is a directory (folder).

4.os.path.isfile(path): determines whether a path is a file.

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

6. os.getcwd(): Get the current working directory.

7. os.path.splitext(file_name): Returns a tuple composed of file name and its suffix.

8. os.path.exists(path): determines whether a path exists.

9. os.path.driname(path): Returns the previous path string of the path.

10. os.remove(file_path): Delete the specified file.

11. os.removedirs(dir_path): Delete the specified empty directory (empty folder).

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