os is the abbreviation of "operating system". As the name suggests, the os module provides an interface for various Python programs to interact with the operating system. By using the os module, on the one hand, you can easily interact with the operating system, on the other hand, the page can greatly enhance the portability of the code. If there is an error in the relevant function of the module, an OSError exception or its subclass exception will be thrown.
note
If you are reading and writing files, it is recommended to use the built-in function open();
If it is a path-related operation, it is recommended to use the submodule os.path of os;
If you want to read multiple files line by line, it is recommended to use the fileinput module;
To create a temporary file or path, it is recommended to use the tempfile module;
To perform more advanced file and path operations, you should use the shutil module.
Of course, the use of the os module to write code that has nothing to do with the operating system does not mean that the os cannot call the extended functions of some specific systems, but it must be remembered that once it does so, it will greatly damage the portability of the code.
In addition, be careful when importing the os module. Never unpack and import the os module for the sake of saving trouble. That is, do not use from os import * to import the os module; otherwise os.open() will overwrite the built-in function open(), resulting in unexpected errors.
Note that most functions in the os module that accept a path as a parameter can also accept a "file descriptor" as a parameter.
File descriptor: file descriptor, abbreviated as fd in Python documentation, is an integer bound to an open file object, which can be understood as the number of the file in the system.
This attribute broadly indicates the environment in which Python is currently running, and is actually the name of the imported operating system-related module. This name also determines which functions in the module are available and which are not implemented accordingly.
Currently valid names are the following three: posix, nt, java.
Among them posix is the abbreviation of Portable Operating System Interface of UNIX (Portable Operating System Interface). Both Linux and Mac OS will return this value; the full name of nt should be "Microsoft Windows NT", which is roughly equivalent to the Windows operating system, so this value will be returned in the Windows environment; java is the return value in the Java virtual machine environment.
Therefore, execute the following code on my computer (win10), the return value is nt:
import os
os.name
' nt'
The result on WSL (Windows Subsystem Linux, Linux subsystem under Windows) is:
import os
os.name
' posix'
Check the sys.platform attribute in the sys module to get more detailed information about the running platform, so I won’t repeat it here.
The os.environ attribute can return environment-related information, mainly various environment variables. The return value is a mapping (similar to a dictionary type), the specific value is a snapshot when the os module is first imported; for each key-value pair, the key is the name of the environment variable, and the value is the value corresponding to the environment variable. After importing the os module for the first time, unless the value of os.environ is directly modified, the value of this attribute will no longer change.
For example, if the key is "HOMEPATH" (under Windows or "HOME" under Linux), the corresponding value is the path of the user's home directory. Under Windows, its value is:
os.environ["HOMEPATH"]'d:\justdopython'
Under Linux, its value is:
os.environ["HOME"]'/home/justdopython'
This function needs to pass in a path as the top parameter. The function of the function is to walk in the directory tree with top as the root node, and generate a three-item composed of (dirpath, dirnames, filenames) for each directory in the tree. Tuple.
Among them, dirpath is a string indicating the path of this directory, dirnames is a list of sub-directory names under dirpath (excluding "." and ".."), and filenames is a list of all non-directory file names under dirpath . It should be noted that these names do not include the path itself. To get the full path of a file or path under dirpath starting from the top directory, you need to use os.path.join(dirpath, name).
Note that the final return result is an iterator. We can use the for statement to get each item of the iterator one by one:
for item in os.walk("."):...print(item)...('.',['do'],['go_go_go.txt'])('.\do',['IAmDirectory','python'],[])('.\do\IAmDirectory',[],[])('.\do\python',[],['hello_justdopython.txt'])
"Listdir" means "list directories", which lists all paths (and files) in the (current) directory. This function has a parameter to specify the path of the subdirectories to be listed. The default is ".", which is the "current path".
The return value of the function is a list, where each element is a string, each path name and file name.
Usually extremely useful in scenarios where you need to traverse files in a certain folder.
For example, define the following function:
def get_filelists(file_dir='.'):
list_directory = os.listdir(file_dir)
filelists =[]for directory in list_directory:
# os.The path module will talk about it later
if(os.path.isfile(directory)):
filelists.append(directory)return filelists
The return value of this function is the name list of all files in the current directory instead of the folder.
"Mkdir", namely "make directory", is used to "create a new path". You need to pass in a classpath parameter to specify the location and name of the new path. If the specified path already exists, a FileExistsError exception will be thrown.
This function can only create a new first-level path under an existing path, otherwise (that is, a new multi-level path) will throw a FileNotFoundError exception.
Correspondingly, in scenarios where a new multi-level path needs to be created, os.makedirs() can be used to complete the task. The function os.makedirs() performs recursive creation. If necessary, it will create a new intermediate path through the specified path until the final "leaf path" is created.
Examples are as follows:
os.mkdir("test_os_mkdir")
os.mkdir("test_os_mkdir")Traceback(most recent call last):
File "<stdin ", line 1,in<module
FileExistsError:[WinError 183]When the file already exists, it cannot be created.:'test_os_mkdir'
os.mkdir("test_os_mkdir/test_os_makedirs/just/do/python/hello")Traceback(most recent call last):
File "<stdin ", line 1,in<module
FileNotFoundError:[WinError 3]System can not find the route.:'test_os_mkdir/test_os_makedirs/just/do/python/hello'
os.makedirs("test_os_mkdir/test_os_makedirs/just/do/python/hello")
Used to delete files. If the specified path is a directory instead of a file, an IsADirectoryError exception will be thrown. To delete a directory, use the os.rmdir() function.
Similarly, corresponding to os.makedirs(), the delete path operation os.rmdir() also has a recursive delete function os.removedirs(), which will try to delete the specified path level by level starting from the lowest level directory, which is almost An inverse process of os.makedirs(); it stops as soon as it encounters a non-empty directory.
The function of this function is to rename the file or path. The general calling format is os.rename(src, dst), that is, the file or path pointed to by src is renamed to the name specified by dst.
Note that if the specified target path is in another directory, this function can also implement the "cut and paste" function of the file or path. But no matter whether you rename directly in place or "cut and paste", the intermediate path must exist, otherwise FileNotFoundError will be thrown. If the target path already exists, FileExistsError will be thrown under Windows; under Linux, if the target path is empty and the user permission allows, the original path will be silently overwritten, otherwise OSError will be thrown.
Like the previous two functions, this function also has a corresponding recursive version os.renames(), which can create missing intermediate paths.
Note that in both cases, if the function is successfully executed, the os.removedir() function will be called to recursively delete the lowest directory of the source path.
"Getcwd" is actually an abbreviation of "get the current working directory", as the name implies, that is to say, the function of this function is to "get the current working directory". During the running of the program, no matter where the program is physically located in the actual storage space, the "current working path" can be regarded as the path where the program is located; the related "relative path", "module import in the same directory" and so on are related All operations are subject to the "current working path".
In an interactive environment, what is returned is the location where the interactive terminal is opened; in a Python file, what is returned is the location of the file.
Under Windows, there will be the following output:
os.getcwd()'d:\justdopython\just\do\python'
The output under Linux is:
os.getcwd()'/home/justdopython/just/do/python'
"Chdir" is actually short for "change the directory", so the purpose of os.chdir() is actually to switch the current working path to the specified path. The "specified path" needs to be passed as a parameter to the function os.chdir(). The parameter can be a text or byte string, a file descriptor, or a generalized class path (path-like ) Object. If the specified path does not exist, FileNotFoundError will be thrown.
Under Windows, the effect of calling this function is:
os.chdir("d:/justdopython/just/do")
os.getcwd()'d:\justdopython\just\do'
The effect under Linux is:
os.chdir("/home/justdopython/just/do") #You can also specify the parameter as".."To switch to the parent directory
os.getcwd()'/home/justdopython/just/do'
With this function, it becomes very convenient to read and write files across directories and call modules. In many cases, it is no longer necessary to copy and paste the same file repeatedly between directories. The script can be in the middle of the army. The completion of the operation of other directory files under the directory is the so-called "During strategizing, the decision is better than thousands of miles away".
For example, you can directly access the file content of the parent directory by switching the "current working directory" to the parent directory:
os.chdir("..")
os.getcwd()'D:\justdopython\just'withopen("hello_justdopython.txt", encoding="utf-8")as f:... f.read()...'Welcome to justdopython.com, learn Python technology together~'
os.listdir()['hello_justdopython.txt']
In fact, this module is imported from another module by the os module according to the system type, and not directly implemented by the os module. For example, if the value of os.name is nt, then import ntpath as path is executed in the os module; if the value of os.name is posix, Then import posixpath.
There is a very important feature to pay attention to when using this module: the functions in os.path are basically pure string operations. In other words, the parameter passed into the module function does not even need to be a valid path, and the module will not try to access the path, but just process the string in accordance with the general format of "path".
Furthermore, the functions of the os.path module can be implemented manually by ourselves using string manipulation. The function of this module is to allow us to implement the same function without considering the specific system, especially without paying too much attention to the file system. The separator problem.
This is a very useful function that can combine multiple incoming paths into one path. In fact, the incoming strings are connected with the system separator and combined into a new string, so the general usage is to use the first parameter as the parent directory, and each parameter after that is the next level directory , Thus combining into a new logical path.
But if there is an "absolute path" format string in the incoming path, and this string is not the first parameter of the function, then all other parameters before this parameter will be discarded, and the remaining parameters will be combined. More precisely, only the last "absolute path" and the following parameters will be reflected in the returned result.
os.path.join("just","do","python","dot","com")'just\do\python\dot\com'
os.path.join("just","do","d:/","python","dot","com")'d:/python\dot\com'
os.path.join("just","do","d:/","python","dot","g:/","com")'g:/com'
Normalize the incoming path and return a corresponding string in absolute path format.
That is to say, when the input path conforms to the format of "absolute path", this function only replaces the path separator with characters suitable for the current system, does not do any other operations, and returns the result. The so-called "format of absolute path" actually refers to the format of a letter plus a colon, followed by a separator and a sequence of strings:
os.path.abspath("a:/just/do/python")'a:\just\do\python'
# There is no drive a in my system
When the specified path does not conform to the above format, the function will automatically obtain the current working path, and use the os.path.join() function to combine it with the passed parameters into a new path string. Examples are as follows:
os.path.abspath("ityouknow")'D:\justdopython\ityouknow'
This function returns the "base name" of the incoming path, that is, the lowest directory of the incoming path.
os.path.basename("/ityouknow/justdopython/IAmBasename")'IAmBasename'
# There is also no such path in my system. Visible os.path.basename()Page is purely string processing
One thing to note about this function is that the returned "base name" is actually the substring after the last separator of the passed path, that is to say, if there is a separator after the lowest directory, the result will be Is an empty string:
os.path.basename("/ityouknow/justdopython/IAmBasename/")''
Contrary to the previous function, it returns the entire string before the last separator:
os.path.dirname("/ityouknow/justdopython/IAmBasename")'/ityouknow/justdopython'
os.path.dirname("/ityouknow/justdopython/IAmBasename/")'/ityouknow/justdopython/IAmBasename'
Haha Actually the first two functions are brothers, this function is the boss.
The function of the function os.path.split() is to divide the incoming path into two strings with the last separator as the boundary, and pack them into a tuple to return; the first two functions os.path.dirname() and The return value of os.path.basename() is the first and second element of the return value of the function os.path.split(). Even the concrete realization of the two is very real:
def basename(p):"""Returns the final component of a pathname"""returnsplit(p)[1]
def dirname(p):"""Returns the directory component of a pathname"""returnsplit(p)[0]
They can be combined to get the original path through the os.path.join() function.
This function is used to determine whether the location pointed to by the path exists. It returns True if it exists, and False if it does not exist:
os.path.exists(".")
True
os.path.exists("./just")
True
os.path.exists("./Inexistence") #Non-existent path
False
The general usage is in the scene where some data needs to be saved persistently. In order to avoid creating a file repeatedly, you need to use this function to check whether the corresponding file exists before writing, if it does not exist, create a new one, if it exists, click on the file content After adding new content.
This function judges whether the incoming path is an absolute path, if it is, it returns True, otherwise it returns False. Of course, only the format is tested, and no verification is performed on its validity:
os.path.isabs("a:/justdopython")
True
These two functions respectively determine whether the incoming path is a file or a path. Note that the validity of the path will be checked here. If it is an invalid path, it will continue to return False.
# Invalid path
os.path.isfile("a:/justdopython")
False
# Effective path
os.path.isfile("./just/plain_txt")
True
# Invalid path
os.path.isdir("a:/justdopython/")
False
# Effective path
os.path.isdir("./just/")
True
This article introduces in detail some commonly used attributes and functions in the os module that interacts with the operating system, which can basically cover the initial learning and use. With these functions, we can already write some more practical scripts.
In addition to the functions introduced in the article, the os module has many more complicated functions, but most of them are not available for us temporarily, and will be further explained in the future.
This article mainly explains the detailed usage of the python standard library OS module. For more knowledge about the python standard library OS module, please see the related links below
Recommended Posts