Home > Article > Backend Development > Python os operation example
1. os.path.driname(path): Returns the upper-level path string of the path.
>>> os.path.dirname('D:\Games')
'D:\\'
>>>
2 . os.path.basename(path): Returns the last-level directory name (folder name) or file name (full name) of the path.
>>> os.path.basename('D:\Games\9yin_632\Snail Whole Package\\0x0804.ini')
'0x0804.ini'
>> ;>
3. os.path.splitext(file_name): Returns a tuple consisting of the file name and its suffix.
>>> os.path.splitext('0x0804.ini')
('0x0804', '.ini')
>>>
4. os.path.abspath(string): Returns the path string of the current working directory plus string.
>>> os.path.abspath('Games') # There is no file or folder "Games" in the current directory, just a random string
'C:\ \Python27\\Games'
>>>
5. os.path.isdir(path): Determine whether a path is a directory (folder).
6. os.path.isfile(path): Determine whether a path is a file.
7. os.listdir(dir_path): Returns all files (full names) and folder names in a directory (dir_path can only be a directory, not a file name path) in the form of a list.
8. os.remove(file_path): Delete the specified file.
9. os.removedirs(dir_path): Delete the specified empty directory (empty folder).
10. os.path.exists(path): Determine whether a path exists.
11. os.mkdir(path): Create a new directory (folder).
12. os.getcwd(): Get the current working directory.
The above is the detailed content of Python os operation example. For more information, please follow other related articles on the PHP Chinese website!