Wu Yuxiong --python study notes: Using the os module

In automated testing, often need to find the file operations, such as to find the configuration file (and thus read the information profile), locate the test report (test report in order to send messages), often operate on a large number of documents and a large number of paths, which os module depends on.
1 . Under the current file path and the path 

os.getcwd (): View current path. 

os.listdir (path): lists all files in the directory. It returns a list of types.

 

import the 

print (os.getcwd ())
 print (os.listdir (os.getcwd ()))
C:\Users\Administrator\pythonwork
['.ipynb_checkpoints', 'turtle.ipynb', 'Untitled.ipynb', 'Untitled1.ipynb', 'Untitled2.ipynb', 'Untitled3.ipynb', 'Untitled4.ipynb']

 

2 Absolute path 

os.path.abspath (path): Returns the absolute path path.
3 . See the path and file name folder part portion 

os.path.split (path): decomposition path (folder, file name), it returns a tuple. As can be seen, if the last character in the string is a path \, only the folder section has a value; if no path string \, only part of a file name value. If there is a string path \, and not in the final, the folder and file names have value. The results and the return of the folder does not contain \. 

Os.path.join (path1, path2, ...): The path combined, if one has an absolute path, the path will be deleted before.
print(os.path.abspath('.'))
C:\Users\Administrator\pythonwork
print(os.path.abspath('..'))
C:\Users\Administrator
print(os.path.split('D:\\pythontest\\ostest\\Hello.py'))
('D:\\pythontest\\ostest', 'Hello.py')
print(os.path.split('.'))
('', '.')
print (os.path.split ( ' D: \\ \\ pythontest ostest \\ ' ))
('D:\\pythontest\\ostest', '')
os.path.split ( ' D: \\ \\ pythontest ostest ' )
print (os.path.split ( ' D: \\ \\ pythontest ostest ' ))
('D:\\pythontest', 'ostest')
print(os.path.join('D:\\pythontest', 'ostest'))
D:\pythontest\ostest
print(os.path.join('D:\\pythontest\\ostest', 'hello.py'))
D:\pythontest\ostest\hello.py
print (os.path.join ( ' D: \\ \\ pythontest b ' , ' D: \\ \\ pythontest a ' ))
D: \ pythontest \ a
print(os.path.dirname('D:\\pythontest\\ostest\\hello.py'))
D:\pythontest\ostest
print(os.path.dirname('.'))
print(os.path.dirname('D:\\pythontest\\ostest\\'))
D:\pythontest\ostest
print(os.path.dirname('D:\\pythontest\\ostest'))
D: \ pythontest
os.path.basename (path): Returns the file name in the path.
print(os.path.basename('D:\\pythontest\\ostest\\hello.py'))
hello.py
print(os.path.basename('.'))

 

 

print(os.path.basename('D:\\pythontest\\ostest\\'))
print(os.path.basename('D:\\pythontest\\ostest'))
ostest
4 view the file time. 

 Os.path.getmtime (path): The last modification time of the file or folder, the number of seconds from the time of access to the new era. 

 os.path.getatime (path): the last access time of a file or folder, the number of seconds from access to a new era. 

 os.path.getctime (path): create a file or folder of time, the number of seconds from access to a new era.
print(os.path.getmtime('F:\\aa.txt'))
1563784321.1284573
print(os.path.getatime('F:\\aa.txt'))
1568359546.3176775
print(os.path.getctime('F:\\aa.txt'))
1568359546.2771735
5 Check the file size. 

  Os.path.getsize (path): size of the file or folder, if the folder returns 0.
print(os.path.getsize('F:\\aa.txt'))
19
print(os.path.getsize('F:\\'))
20480
6 . Check the file exists 

 os.path.exists (path): a file or folder exists, return True or False.
print (os.listdir (os.getcwd ()))
['.ipynb_checkpoints', 'turtle.ipynb', 'Untitled.ipynb', 'Untitled1.ipynb', 'Untitled2.ipynb', 'Untitled3.ipynb', 'Untitled4.ipynb']
print(os.path.exists('D:\\pythontest\\ostest\\hello.py'))
False
print(os.path.exists('D:\\pythontest\\ostest\\Hello.py'))
False
print(os.path.exists('D:\\pythontest\\ostest\\Hello1.py'))
False
7 . Some form of parameters 

os defines a set of files, the path forms at different operating system parameters, such as:
print (os.sep)
\
print (os.extsep)
.
print (os.pathsep)
;
print (os.linesep)
8 . Examples illustrate 

the automated testing process, often need to send e-mail will be sent the latest test report document to the relevant personnel to view, it is you need to find the features in the latest files. 

Example: Find the latest file folder.

 

 

Import os
 DEF new_file (test_dir):
     # list all files in the directory test_dir (name), results are returned in a list. 
    = lists os.listdir (test_dir)
     # the Sort by keyword performed in ascending order of the key elements of the lambda parameter fn as a list of lists, access to the file was last modified, the final time to file in ascending order 
    # finally lists elements, by file modification times the size from small to large. 
    lists.sort (Key = the lambda the Fn: os.path.getmtime (test_dir + ' \\ ' + the Fn))
     # get the absolute path of the latest document, the last value in the list, folder + filename 
    file_path = os.path.join (test_dir, Lists [-1 ])
     return file_path 

# returns D: \ pythontest \ latest file ostest following 
Print new_file ( 'D:\\system files\\workspace\\selenium\\email126pro\\email126\\report')

 

 

Usage of a lambda (minimum one-way function in Python):
key=lambda fn:os.path.getmtime(test_dir+'\\'+fn)
#相当于
def key(fn):
    return os.path.getmtime(test_dir+'\\'+fn)

 

Guess you like

Origin www.cnblogs.com/tszr/p/11929526.html