Posts

Showing posts from July, 2015

Python: Let’s call “datetime”!

For QA engineer very often situation, when need keep test results created by scheduling. I would like to share with you, one very simple and the same time very powerfull tool. It’s calling “datetime”. This module will add timestamp inside your code. In your input you can see next attributes: date, time (hours, minutes, seconds and microseconds) and also timezone information if need. Also, for more fast align output files in you file browser you can include that information inside a name of created output file. Quick Example: [code language="python"] import datetime t = datetime.date.today() y = datetime.datetime.today() f = open(str(t) + ‘.datime.log’, ‘a’) # 'a' - will add new test results inside existing file. # Task a = 3 b = 3 c = a * b # Action f.write(‘\nData: ‘ + str(y)) f.write(‘\nResult: ‘ + str(c)) f.close() [/code] Output: Data: 2015-10-26 09:35:59.941000 Result: 9

Python: fnmatch – Compare filenames by patterns.

[code language="python"] """ "fnmatch" module compares file names against glob-style patterns such as used by Unix shells. Find more information: https://pymotw.com/2/fnmatch/ http://www.pythonforbeginners.com/fnmatch/os-walk-and-fnmatch-in-python """ import os from fnmatch import fnmatch root = '//Users//computerLoginName//Documents//' extension = 'test*.py' counts = 0 for path, dirs, files in os.walk(root): for name in files: if fnmatch(name, extension): print(os.path, name) # count repeated files inside directory #num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))]) #print(num_files) # count number of files counts += 1 print(counts) [/code]