Best Python code snippet using autotest_python
genio.py
Source:genio.py  
...29def log_line(filename, line):30    """31    Write a line to a file.32    :param filename: Path of file to write to, either absolute or relative to33                     the dir set by set_log_file_dir().34    :param line: Line to write.35    """36    global _open_log_files, _log_file_dir  # pylint: disable=W060337    path = utils_path.get_path(_log_file_dir, filename)38    if path not in _open_log_files:39        # First, let's close the log files opened in old directories40        close_log_file(filename)41        # Then, let's open the new file42        try:43            utils_path.init_dir(os.path.dirname(path))44        except OSError:45            pass46        _open_log_files[path] = open(path, "w")47    timestr = time.strftime("%Y-%m-%d %H:%M:%S")48    _open_log_files[path].write("%s: %s\n" % (timestr, line))49    _open_log_files[path].flush()50def set_log_file_dir(directory):51    """52    Set the base directory for log files created by log_line().53    :param dir: Directory for log files.54    """55    global _log_file_dir  # pylint: disable=W060356    _log_file_dir = directory57def close_log_file(filename):58    global _open_log_files, _log_file_dir  # pylint: disable=W060359    remove = []60    for k in _open_log_files:61        if os.path.basename(k) == filename:62            f = _open_log_files[k]63            f.close()64            remove.append(k)...utils_logfile.py
Source:utils_logfile.py  
...32def log_line(filename, line):33    """34    Write a line to a file.35    :param filename: Path of file to write to, either absolute or relative to36                     the dir set by set_log_file_dir().37    :param line: Line to write.38    :raise LogLockError: If the lock is unavailable39    """40    global _open_log_files, _log_file_dir, _log_lock41    if not _acquire_lock(_log_lock):42        raise LogLockError("Could not acquire exclusive lock to access"43                           " _open_log_files")44    log_file = get_log_filename(filename)45    base_file = os.path.basename(log_file)46    try:47        if base_file not in _open_log_files:48            # First, let's close the log files opened in old directories49            close_log_file(base_file)50            # Then, let's open the new file51            try:52                os.makedirs(os.path.dirname(log_file))53            except OSError:54                pass55            _open_log_files[base_file] = open(log_file, "w")56        timestr = time.strftime("%Y-%m-%d %H:%M:%S")57        try:58            line = string_safe_encode(line)59        except UnicodeDecodeError:60            line = line.decode("utf-8", "ignore").encode("utf-8")61        _open_log_files[base_file].write("%s: %s\n" % (timestr, line))62        _open_log_files[base_file].flush()63    finally:64        _log_lock.release()65def set_log_file_dir(directory):66    """67    Set the base directory for log files created by log_line()68    :param directory: Directory for log files69    """70    global _log_file_dir71    _log_file_dir = directory72def get_log_file_dir():73    """74    Get the base directory for log files created by log_line()75    """76    global _log_file_dir77    return _log_file_dir78def get_log_filename(filename):79    """...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
