First, the Python OS module introduction
The OS module is simply a python system-programmed operating module that can handle files and directories that we manually need to do on a daily basis.
You can view the Help documentation for the OS module:
>>> Import OS #导入os模块
>>> Help (OS) #查看os模块帮助文档, with detailed module-related functions and how to use them
Second, the OS module important functions and variables:
1), OS.SEP change the path delimiter in the operating system.
2), OS.GETCWD () Gets the current path, which is more commonly used in Python code.
3), Os.listdir () lists all files and folders in the current directory.
4), Os.remove () method to delete the specified file.
5), the Os.system () method is used to run the shell command.
6), Os.chdir () change the current directory to the specified directory.
third, the OS module function of the detailed
The Os.system function can run the Shello command, which is the command in the terminal emulator on the Linux system.
There are also functions that can execute external programs, including EXECV, which exits the Python interpreter and gives control to the executed program.
The OS.SEP variable is used primarily for separators in the system path.
Windows system through is "\ \", Linux class system such as Ubuntu separator is "/", and Apple Mac OS system is ":".
Iv. OS module Case code Demo
Summary:OS module in the system files and directory operations used more, is also our common module, we must master the knowledge, which is a typical Python standard cross-platform module, support Windows and Linux system operations, without modifying any code, No errors are generated.
In automated testing, often need to find operations files, such as looking for configuration files (thus reading the configuration file information), to find test reports (thus sending test report messages), often to a large number of files and a large number of paths to operate, which depends on the OS module, so today to collate the more commonly used methods. This information on the Internet is also a lot, every time, just the knowledge they learned to comb, thereby deepening the use of a module.
1. Files under current path and path
OS.GETCWD (): View the current path.
Os.listdir (PATH): Lists all files under the directory. The type of the list is returned.
>>> import os>>> os.getcwd () ' D:\\pythontest\\ostest ' >>> os.listdir (OS.GETCWD ()) [' hello.py ', ' test.txt ']
2. Absolute path
Os.path.abspath (PATH): Returns the absolute path.
>>> os.path.abspath ('. ') ' D:\\pythontest\\ostest ' >>> os.path.abspath (' ... ') ' D:\\pythontest '
3. View the folder part and file name section of the path
Os.path.split(path): Breaks the path into (folder , file name ) and returns the tuple type. As you can see, if the last character of the path string is \, only the folder part has a value, and if none in the path string, only the file name part has a value. If the path string has \ and is not at the end, both the folder and the file name have values. And the result of the returned folder does not contain \.
Os.path.join (path1,path2,...): combines path and, if there is an absolute path, the previous path is deleted.
>>> os.path.split (' d:\\pythontest\\ostest\\hello.py ') (' d:\\pythontest\\ostest ', ' hello.py ') >>> Os.path.split ('. ') (‘‘, ‘.‘) >>> os.path.split (' d:\\pythontest\\ostest\\ ') (' d:\\pythontest\\ostest ', ') >>> os.path.split (' D : \\pythontest\\ostest ') (' d:\\pythontest ', ' ostest ') >>> os.path.join (' d:\\pythontest ', ' ostest ') ' d:\\ Pythontest\\ostest ' >>> os.path.join (' d:\\pythontest\\ostest ', ' hello.py ') ' d:\\pythontest\\ostest\\ hello.py ' >>> os.path.join (' d:\\pythontest\\b ', ' d:\\pythontest\\a ') ' d:\\pythontest\\a '
Os.path.dirname (PATH): Returns the folder portion of path with the result not containing ' \ '
>>> os.path.dirname (' d:\\pythontest\\ostest\\hello.py ') ' D:\\pythontest\\ostest ' >>> Os.path.dirname ('. ') ' >>> os.path.dirname (' d:\\pythontest\\ostest\\ ') ' D:\\pythontest\\ostest ' >>> os.path.dirname (' D : \\pythontest\\ostest ') ' D:\\pythontest '
Os.path.basename (PATH): Returns the file name in path.
>>> os.path.basename (' d:\\pythontest\\ostest\\hello.py ') ' hello.py ' >>> os.path.basename ('. ') '. ' >>> os.path.basename (' d:\\pythontest\\ostest\\ ') ' >>> os.path.basename (' D:\\pythontest\\ostest ') ' Ostest '
4. View file time
Os.path.getmtime (PATH): The last modification time of a file or folder, from the new era to the number of seconds it was accessed.
Os.path.getatime (PATH): The last access time of a file or folder, from the new era to the number of seconds it was accessed.
Os.path.getctime (PATH): The time the file or folder was created, from the new era to the number of seconds it was accessed.
>>> os.path.getmtime (' d:\\pythontest\\ostest\\hello.py ') 1481695651.857048>>> os.path.getatime ( ' d:\\pythontest\\ostest\\hello.py ') 1481687717.8506615>>> os.path.getctime (' d:\\pythontest\\ostest\\ hello.py ') 1481687717.8506615
5. View File Size
Os.path.getsize (PATH): The size of the file or folder, if the folder returns 0.
>>> os.path.getsize (' d:\\pythontest\\ostest\\hello.py ') 58l>>> os.path.getsize (' d:\\pythontest\ \ostest ') 0L
6. See if the file exists
Os.path.exists (PATH): whether the file or folder exists, returns True or False.
>>> Os.listdir (OS.GETCWD ()) [' hello.py ', ' Test.txt ']>>> os.path.exists (' d:\\pythontest\\ostest\\ hello.py ') true>>> os.path.exists (' d:\\pythontest\\ostest\\hello.py ') true>>> os.path.exists (' D : \\pythontest\\ostest\\Hello1.py ') False
7. Some representation parameters
The OS defines a set of file and path representation parameters in different operating systems, such as:
>>> os.sep ' \ \ ' >>> os.extsep '. ' >>> os.pathsep '; ' >>> os.linesep ' \ r \ n '
8. Example Description
During automated testing, it is often necessary to send a message to send the latest test report document to the relevant people, which is the ability to find the latest files.
Example: Find the most recent file under a folder.
The code is as follows:
Import osdef New_file (test_dir): #列举test_dir目录下的所有文件 (name), the result is returned as a list. Lists=os.listdir (test_dir) #sort按key的关键字进行升序排序, the lambda's entry fn is the element of the lists list, obtaining the last modified time of the file, so the file time is eventually sorted from small to large #最后对lists元素, sort by file modification time size from small to large. Lists.sort (Key=lambda fn:os.path.getmtime (test_dir+ ' \ \ ' +fn)) #获取最新文件的绝对路径, the last value in the list, folder + file name File_ Path=os.path.join (Test_dir,lists[-1]) return file_path# return D:\pythontest\ostest below the latest file print new_file (' d:\\ System Files\\workspace\\selenium\\email126pro\\email126\\report ')
Operation Result:
In the end, say something about lambda usage (the smallest function of a single line in Python):
Key=lambda fn:os.path.getmtime (test_dir+ ' \ \ ' +fn) #相当于def key (FN): return os.path.getmtime (test_dir+ ' \ \ ' +fn)
Reprinted from: http://www.iplaypy.com/module/os.htmlhttps://www.cnblogs.com/yufeihlf/p/6179547.html
Python's OS module