Home  >  Article  >  Backend Development  >  Python os operation example

Python os operation example

PHP中文网
PHP中文网Original
2017-06-21 17:00:00945browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn