os, as its name implies, is the standard library related to the operating system. Such as files, directories, executing system commands, etc.
1. Import module
os is a python standard library module. It can be installed with Python without separate installation and can be imported directly.
2. path sub module
When it comes to disk file operations, the most commonly used module is the path module. Path is a sub module of OS. It can be used through from os import path or directly through the os.path attribute. In order to maintain consistency, this paper adopts the writing form of the latter.
Detect whether the file or directory exists. Returns True if it exists and False if it does not exist.
Determine whether it is a file. Returns True, not False. It can also be used to determine whether the file exists.
Determine whether it is a directory. Returns True, not False. It can also be used to determine whether a directory exists.
Returns the file name (including extension) that does not contain the directory.
Returns the directory where the file is located.
Returns a tuple. The first element of tuple is the directory where the file is located, and the second element is the file name (including extension). Equivalent to (dirname(path), basename(path)).
os.path.split("dir1/dir2/file.ext") ('dir1/dir2', 'file.ext')
Returns a tuple. The first element of the tuple is the directory and file name of the file (without extension), and the second element is the extension (including.). Commonly used to read or change file extensions.
os.path.splitext("dir1/dir2/file.ext") ('dir1/dir2/file', '.ext')
2.8 join(path, *paths)
Splice different parts of the path into a complete path. Equivalent to os.sep.join([path, *paths]).
os.path.join("dir1", "dir2", "file.ext") 'dir1/dir2/file.ext'
Returns the file size. Unit byte.
3. Directory operation
Returns a list. The list is all files and subdirectories in the given directory, but does not contain special directories. And. The default is the current directory.
os.listdir("dogs")[:5] ['122.Pointer', '069.French_bulldog', '124.Poodle', '112.Nova_scotia_duck_tolling_retriever', '043.Canaan_dog']
3.2 mkdir(path, mode=0o777)
Create a directory named path. And specify directory permissions in numerical form. The default permission is 777.
3.3 makedirs(path, mode=0o777)
Create a directory with path recursively. And specify directory permissions in numerical form. The default permission is 777. It can be regarded as a more powerful mkdir. It will automatically create all parent directories of the leaf node directory. mkdir can only create the leaf node directory when the parent directory already exists.
Delete directory. The directory must exist and only empty directories can be deleted. If it does not exist or is not empty, an exception will occur. To recursively delete the entire directory tree, use shutil.rmtree().
Recursively delete directories. The directory must exist and only empty directories can be deleted. If it does not exist or is not empty, an exception will occur. Different from rmdir, after deleting the leaf node directory, the parent directory will be deleted one by one until a directory that is not empty is encountered.
Delete file. The directory cannot be deleted. The given path must be a file, otherwise an exception will occur.
# Copy file with open("dog.jpeg", "rb") as f: content = f.read() with open("dog.copy.jpeg", "wb") as f2: f2.write(content) # Delete file os.remove("dog.copy.jpeg")
4. Other os interfaces
4.1 getenv(key, default=None)
Gets the environment variable.
Returns a list of directories used to search for executable files. Look at it as a list of PATH environment variables.
os.get_exec_path() ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']
In the current process, start the child process and execute the command (string). The main process will block until the execution of the child process is completed. This is achieved by calling the standard C function system() with the same restrictions.
if os.name == "nt": command = "dir" else: command = "ls -l" os.system(command) 0