Use of Python's os library

巴扎黑
Release: 2016-12-08 09:39:53
Original
1726 people have browsed it

The python os library has many functions related to the operating system.

In fact, it’s not just that. There are many things related to files, paths, and execution of system commands in the os library.

The following are the commonly used methods of the os module.

1.os.sep                                                                                                                                                             using os module. For example, for Windows, it is 'nt', and for Linux/Unix users, it is 'posix'
3.os.getcwd() The function gets the current working directory, which is the directory path where the current Python script works

4.os. Getenv () is used to read environmental variables

5.OS.Putenv () used to set environment variables

6.os.listdir () Return all files and directory names in the specified directory
7os.remove () function for Delete a file
8.os.system() The function is used to run shell commands

9.os.linesep The string gives the line terminator used by the current platform. For example, Windows uses '/r/n', Linux uses '/n' and Mac uses '/r'.

10.os.path.split() The function returns the directory name and file name of a path

11.os.path.isfile() Verify that the given path is a file

12os.path.isdir() Verify The output path is a directory

13.os.path.existe() The function is used to check whether the given path actually exists

14.os.listdir(dirname) Lists the directories and files under dirname

15. os.getcwd() Get the current working directory
16.os.curdir Return the previous directory ('.')
17.os.chdir(dirname) Change the working directory to dirname

os.path is specified with the path Functions related to files, directories, and path strings

os.path.isdir(name) Determines whether name is a directory, returns false if name is not a directory

os.path.isfile(name) Determines whether name is a directory File, if name does not exist, it will return false
os.path.exists(name) Determine whether the file or directory name exists
os.path.getsize(name) Get the file size, if name is a directory, return 0L
os.path.abspath( name) Get the absolute path
os.path.normpath(path) Normalize the path string form
os.path.split(name) Split the file name and directory (in fact, if you use the directory entirely, it will also split the last directory Separated as a file name, and it does not determine whether the file or directory exists)
os.path.splitext() Separates the file name and extension

os.path.join(path,name) Joins the directory and file name Or directory

os.path.basename(path) Returns the file name
os.path.dirname(path) Returns the file path


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template