Get path & Directory add file

In the process of automated testing , Consider moving engineering files or running them in someone else's work environment , So our path should be flexible , You can't write the path dead .

Recommended Python The built-in modules OS

Refer to the picture

import unittest
from common.HTMLTestRunner_cn import HTMLTestRunner
import os
# Current script path
curPath = os.path.realpath(__file__)
# Current script folder name
proPath = os.path.dirname(curPath)
# Path of test case
startCase = os.path.join(proPath, "test_case")
# Path to test report
reportPath = os.path.join(proPath, "report", "report.html")

  The operation of the path

# Get current directory 
# Get the superior Directory
print(os.path.abspath(os.path.join(os.getcwd(), "..")))
# Get the superior Directory
print(os.path.abspath(os.path.join(os.getcwd(), "../..")))

Get the latest files

In the process of testing, we will execute many times and generate many test reports , So how to get the latest test report ?

Case diagram :

Pictured ,report There are a lot of test reports

import os
# Current script folder name
proPath = os.path.dirname( os.path.realpath(__file__))
print(proPath) def get_newest_report(report_path):
# list report_path All the files in the directory ( name ), The result returns... As a list
lists =os.listdir(report_path)
# sort Press key In ascending order ,lambda Input fn by lists Elements of the list , retrievable Last modified , So we sort the files from small to large in the end
# Finally, lists Elements , Sort by file modification time from small to large .
# Get the absolute path to the latest file , The last value in the list , Folder + file name
#lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn))) # Two ways to sort
lists.sort(key=lambda fn: os.path.getmtime(report_path+"\\"+fn)) # Two ways to sort
print(u' The latest test generated reports : '+lists[-1])
report_file = os.path.join(report_path, lists[-1])
return report_file if __name__ == "__main__":
report_path = os.path.join(proPath, "report") # Report Directory (report_path)
report_file = get_newest_report(report_path) # return report_path The latest file

os.path.getmtime And os.path.getctime The difference between :

import os
import time
file = 'C:\\Users\Administrator\PycharmProjects\\unittest_lianxi'
print(os.path.getatime(file))# Output the latest access time
print(os.path.getctime(file))# Output file creation time
print(os.path.getmtime(file))# Output the latest modification time
print(time.gmtime(os.path.getmtime(file)))# With struct_time The form outputs the latest modification time
print(os.path.getsize(file)) # Output file size ( Bytes are units )
print(os.path.abspath(file)) # Output absolute path
print(os.path.normpath(file) )

Python built-in os Module simple and practical more related articles

  1. Python built-in OS Detailed explanation of module usage

    Hello everyone , Get up early today Python Will continue to update by Xiaotian students from the perspective of beginners Python The notes , Its characteristic is that most of the full text is easy to understand by the novice code and comments and dynamic demonstration . New readers must not miss ! Many people study python, Ignorance ...

  2. Python Built in standard module

    time modular 1 >>> import time 2 >>> time.time() 3 1491064723.808669 4 >>> # t ...

  3. $Django Send E-mail --django Package modules and python built-in SMTP modular

    One Use SMTP Module send mail import smtplib from email.mime.text import MIMEText from email.header import Header m ...

  4. Python built-in logging modular

    Recently debugged Python More code , Find out logging The module is easy to use . My usage is as follows : import logging logging.basicConfig(level=logging.INFO, forma ...

  5. csv The document has python built-in csv modular

    csv(Comma Separated Value, Comma separated values ), Files store table data in plain text ( Numbers and text ). It can be used excel open , And automatically put each comma separated data as a column in excel It shows that . pytho ...

  6. Python built-in GUI modular Tkinter A few notes of

    Component properties , usage Component location more

  7. day25 Built in common modules ( Four ): Modules and packages

    Read the directory : modular import from xxx import xxx package import from xxx import xxx    from xxx  import *    __init__.p ...

  8. Talking about Python Built in object types —— Numbers ( attach py2 and py3 One of the differences )

    Python It's an object-oriented programming language , Everything in a program can be treated as an object .Python Built in objects can be divided into simple types and container types , Simple types are mainly numerical data , A container type is a collective that can contain other object types , Such as sequence . ...

  9. python What are built-in constants ?

    Abstract : Study Python In the process of , We'll start with variable constants , that python Do you know the built-in constants ? A new product , Want to get familiar with it , The best way is to look at the manual ! you 're right ,Python We have also prepared such a manual for us --Pyt ...

Random recommendation

  1. 【 Record of writing questions 】 First division attached NOIP practice 20160820

    [ Because the teacher won't let me have the topic ,So I can only send test sites !!!! Lulu, Lulu ] [ subject ( SHH ~~~~~)]: Baidu cloud Download [T1]: Seems to be DP, Path compression + Sort + Handle according to the situation 100 branch ( Lulu, Lulu ) [T2]: It's hard to solve ,DFS It's a mess ...

  2. SQL SERVER 9003 Error resolution Only applicable to SQL2000

    SQLSERVER 9003 Error resolution Only applicable to SQL2000 ( Only applicable to SQL2000) " Unable to open new database 'POS'.CREATE DATABASE suspend . (Microsoft S ...

  3. Apache and PHP Installation

    Recently I read some textbooks , Maybe it's the printing , It's just a space that doesn't print , In addition, many blog articles on the Internet are just reproduced conveniently , I didn't check it carefully , also php and apache Version issues between , It cost me 4,5 It takes about two hours to match the environment . ...

  4. Android -- View setScale, setTranslation Yes View Matrix processing

    Reference resources : 1.Android Matrix Theory and application 2.2D About matrices in the plane (Matrix) With the explanation of graphic transformation 3.Android About matrices in (Matrix) Some knowledge about the front and back rides 4.Android Ma ...

  5. Handler Cause memory leaks

    Let's start with a simple set of code 1 2 3 4 5 6 7 8 9 public class SampleActivity extends Activity { private final Handler mHa ...

  6. hibernate One on one It's one to many , More than one foreign key is unique unique package; import; import java. ...

  7. Google Chrome 55 Released – Install on RHEL/CentOS 7/6 and Fedora 25-20

    Google Chrome is a freeware web browser developed by Google Inc. Google Chrome team proudly announce ...

  8. iOS Common statements in development @synthesize obj = _obj The meaning of

    We're doing iOS When developing , You often see something like @synthesize window=_window;  The sentence of , that , This window What is it? ,_ window What is it again? , How to use the two things separately , This is one ...

  9. hdu 4499 Cannon( violence )

    Topic link :hdu 4499 Cannon The main idea of the topic : Give a n*m The chessboard of , It's already there k A chess piece , Give the position of the pieces , And then how many guns can be put on this chessboard , Guns placed after the request cannot attack each other . Their thinking : enumeration ...

  10. FastDFS And Nginx Configuration description of

    1. brief introduction     FastDFS Is an open source lightweight distributed file system , It manages files , Features include : File store . File synchronization . File access ( Upload files . File download ) etc. , The problem of large capacity storage and load balancing is solved . It's especially suitable for documents ...