Automatic quick start -- python(20) -- processing files and directories

This is Qing'an. Why do you want to talk about this article? It is mainly because it will be used in the preparation of automatic test framework or script. It is relatively convenient.

catalogue

Process file and directory names

Split path

List the contents of the catalogue

Get file meta information

Construct absolute path

Get started with examples

Python 3 has a module called os, which stands for "operating system" os module contains many functions to obtain (and modify) the information of local directory, file process, environment variables, etc.

import os

print(os.getcwd())


path = r"F:\selenium_demo\shopX_demo\casedata"
os.chdir(path)
print(os.getcwd())

Look at the above two functions. getcwd(),chdir(). Use OS The getcwd() function obtains the current working directory. Use OS The chdir() function changes the current working directory. So don't forget to specify a file path when using the chdir () function.

F:\selenium_demo\shopX_demo\index
F:\selenium_demo\shopX_demo\casedata

Process file and directory names

import os

print(os.path.join(r"F:\selenium_demo\shopX_demo\index", "book.py"))
print(os.path.join(r"F:\selenium_demo\shopX_demo\index/", "book.py"))
print(os.path.join(os.path.expanduser('~')))
print(os.path.join(os.path.expanduser('~'), "jmeter.log"))

Look at these three examples. os. path. The join () function constructs a path name from one or more path fragments. In this case, it is simply a concatenated string.

The second example is to add a different slash, but the problem is not big. The paths can be spliced and the files can still be accessed.

        os.path.expanduser() is used to include the ~ symbol (indicating the current user's Home directory) on any operating system with the concept of Home directory (including Linux, Mac OS X and Windows). This function can work.

Split path

path = r'F:\selenium_demo\shopX_demo\index\book.py'
print(os.path.split(path))
(name, filename) = os.path.split(path)
print(name, "The name is:"+filename)
(shortname, extension) = os.path.splitext(filename)
print(shortname, "Suffix:" + extension)

In the first print, the split function splits a full path and returns the directory and file name.

The second print is whether you look at the variable or your scalp is numb. This is a way of writing. Just know. When multiple values, multivariable assignment should be used to assign the return value of split function to a binary. Each variable obtains the value of the corresponding element in the returned tuple.

The third print obtains the file name of the second print, and we use OS path. The splitext() function splits a file name and returns a short file name and extension. The same technique can be used to assign their values to different variables.

('F:\\selenium_demo\\shopX_demo\\index', 'book.py')
F:\selenium_demo\shopX_demo\index The name is: book.py
book Suffix:.py

List the contents of the catalogue

The first is to confirm that there are files in the directory. essential. Secondly, glob module is another tool in Python standard library. It can obtain the contents of a directory through programming, and it uses wildcards under the familiar command line.

import glob

print(glob.glob('*qing*.py'))

# ['qing.py', 'qingan.py']

File I created, Qing py and Qingan py file. The above code, * is used for matching. You can match whether you are a 1qing or qing1 file. Secondly, I added the requirement of matching py type file. Therefore, it automatically matches all the bands Qing. In the current file py file.

Get file meta information

Every modern file system stores meta information for files: Creation time, last repair Change time, file size, etc. So how do we get the view.
import os
import time

i = os.stat('book.py')
print(i)
o = time.localtime(i.st_mtime)
print(o)
os.stat_result(st_mode=33206, st_ino=4503599627371288, st_dev=2027176494, st_nlink=1, st_uid=0, st_gid=0, st_size=100, st_atime=1643383767, st_mtime=1643383767, st_ctime=1637983494)
time.struct_time(tm_year=2022, tm_mon=1, tm_mday=28, tm_hour=23, tm_min=29, tm_sec=27, tm_wday=4, tm_yday=28, tm_isdst=0)

        i = os. The output result of stat ('Book. Py ') can clearly see some information in it, St_ Mtime last modification time, so we need more detailed information about the last modification time. What should we do? time.localtime(i.st_mtime), timestamp call. os. St of the return value of stat() function_ Mtime attribute) into a more useful structure containing year, month, day, hour, minute and second.

Construct absolute path

        os. path. The realpath() function is very simple. Look at the example directly:

import os

print(os.path.realpath('book.py'))
F:\selenium_demo\shopX_demo\index\book.py

In this way, you can get the absolute path of a file.

Get started with examples

import os

class Config:
    ''' 
    Relative paths of all files under the project
    '''
    Base_Path = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + '/..')
    Base_readd = Base_Path + r'\casetest\case02.xlsx'
    ChromeDriver_Path = Base_Path + r'\lib\chromedriver.exe'
    FirefoxDriver_Path = Base_Path + r'\lib\geckodriver.exe'

re = Config()

This is just an example. In this way, you can directly locate the current file and directly locate the folder or file you want to locate through path splicing. It's quite convenient. You can also use the knowledge points mentioned above.

Keywords: Python

Added by digioz on Wed, 09 Feb 2022 15:10:19 +0200