Gain insight into the OS module in Python

Source: Internet
Author: User
This article to share the content is in-depth understanding of Python OS module, has a certain reference value, the need for friends can refer to

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)

Related recommendations:

OS module usage in python

Python Module Learning OS module

Python's OS module

Python OS Module Manual

How to use the OS module in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.