Article directory
(Related free learning recommendations: python video tutorial)
os
module and shutil
module are Python processing files / directory's primary mode. The os module provides a convenient way to use operating system-related functions, and the shutil module is an advanced file/directory operation tool.
File processing
os
The module provides some convenient functions to use operating system resources, such as reading files in the resource directory files, view all contents of files under a certain path on the command line, etc.
Get the system type
When developing code compatibility to adapt to different operating systems, it can be easily solved by judging the operating system type.
import osimport sysprint(os.name) # 返回nt代表Windows,posix代表Linuxprint(sys.platform) # 更详细信息
Get the system environment
When setting environment variables, the module environ is often called
Module. os.environ
returns system environment variables in the form of a dictionary. To obtain specific attribute values, you can use the index or the method getenv()
:
import osprint(os.environ)print(os.environ['PATH'])print(os.getenv('PATH'))
Execute system commands
Use the os modulesystem()
method to execute shell commands, normal execution will return 0. The usage format is os.system("bash command")
.
When writing in non-console mode, system()
will only call the system command but not execute it. The execution result can be returned through the popen()
function The file
object is read and obtained.
import os os.system('ping www.baidu.com')os.popen('ping www.baidu.com').read()
Operation directories and files
One of the most common functions of Python development when using the os module to operate directories and files one.
Method | Description | Example |
os.getcwd() | Get the current directory path | |
Change the current script directory | ||
List all files in the directory | ||
Create a single directory | ||
Create a multi-level directory | ||
Delete a single-level empty directory | ||
Delete multi-level directories | ##os.rename("File or directory name", "Target name") | |
##os.path.abspath() | ||
os.path.split(path) | ||
If there is no \ in the path string, only the file name part has a value; | If the path string contains \ and is no longer the last, then the folder and file names all have values.
|
##os.path.join(path1,path2) | Combining paths
os.path.dirname(path) | Get the folder part in path | |
os.path.basename( path) | Get the file name in path||
os.path.exists(path) | Judge whether the file or folder exists | |
os.path.isfile(path) | Determine whether the path is a file||
os.path.isdir(path) | Determine whether the path is a directory||
os.path.getsize(path) | Get file or folder size||
##os.path.getctime(path) | Get the file or folder creation time | |
os.path.getatime(path) | Get the file or Folder last access time | |
##os.path.getmtime(path) | Get the last modification time of a file or folder | |
os.sep() | Path separator | |
os.extsep() | Separator between file name and suffix | |
Path separator | ||
Newline symbol | ||
The above is the detailed content of Python explains the os module and shutil module in detail. For more information, please follow other related articles on the PHP Chinese website!