How to use remove_files_if_exists method in locust

Best Python code snippet using locust

test_web.py

Source:test_web.py Github

copy

Full Screen

...354 STATS_HISTORY_FILENAME = "{}_stats_history.csv".format(STATS_BASE_NAME)355 STATS_FAILURES_FILENAME = "{}_failures.csv".format(STATS_BASE_NAME)356 def setUp(self):357 super().setUp()358 self.remove_files_if_exists()359 parser = get_parser(default_config_files=[])360 self.environment.parsed_options = parser.parse_args(["--csv", self.STATS_BASE_NAME, "--csv-full-history"])361 self.stats = self.environment.stats362 self.stats.CSV_STATS_INTERVAL_SEC = 0.02363 locust.stats.CSV_STATS_INTERVAL_SEC = 0.1364 self.stats_csv_writer = StatsCSVFileWriter(365 self.environment, stats.PERCENTILES_TO_REPORT, self.STATS_BASE_NAME, full_history=True366 )367 self.web_ui = self.environment.create_web_ui("127.0.0.1", 0, stats_csv_writer=self.stats_csv_writer)368 self.web_ui.app.view_functions["request_stats"].clear_cache()369 gevent.sleep(0.01)370 self.web_port = self.web_ui.server.server_port371 def tearDown(self):372 super().tearDown()373 self.web_ui.stop()374 self.runner.quit()375 self.remove_files_if_exists()376 def remove_file_if_exists(self, filename):377 if os.path.exists(filename):378 os.remove(filename)379 def remove_files_if_exists(self):380 self.remove_file_if_exists(self.STATS_FILENAME)381 self.remove_file_if_exists(self.STATS_HISTORY_FILENAME)382 self.remove_file_if_exists(self.STATS_FAILURES_FILENAME)383 def test_request_stats_full_history_csv(self):384 self.stats.log_request("GET", "/test", 1.39764125, 2)385 self.stats.log_request("GET", "/test", 999.9764125, 1000)386 self.stats.log_request("GET", "/test2", 120, 5612)387 greenlet = gevent.spawn(self.stats_csv_writer.stats_writer)388 gevent.sleep(0.01)389 self.stats_csv_writer.stats_history_flush()390 gevent.kill(greenlet)391 response = requests.get("http://127.0.0.1:%i/stats/requests_full_history/csv" % self.web_port)392 self.assertEqual(200, response.status_code)393 self._check_csv_headers(response.headers, "requests_full_history")...

Full Screen

Full Screen

pyfile.py

Source:pyfile.py Github

copy

Full Screen

...45 # 如果存在同名文件,但是不允许覆盖重写,就报错46 elif os.path.isfile(path) and not overwrite:47 raise Exception('该目录下已经存在同名的文件,不可创建与文件同名的文件夹,'48 'overwrite=True可以先删除该文件后创建:' + path)49 def remove_files_if_exists(self, path, file_prefix=None, file_suffix=None):50 """51 删除path下的指定格式文件。52 :param path: 目录或者文件名53 :param file_prefix: 如果文件名满足这个前缀,则删除54 :param file_suffix: 如果文件名满足这个后缀,则删除55 如果没有指定前缀和后缀,说明path的文件名,只需要删除这个文件56 """57 # file_prefix='__init'58 # file_suffix='.py'59 # 判断是否存在60 if not os.path.exists(path):61 print('指定的目录或者文件不存在,不需要执行删除操作:' + path)62 return63 # 如果是文件,则删除64 if os.path.isfile(path):65 os.remove(path)66 return67 # 否则就是删除指定前缀或者后缀的文件68 # 先获取全部文件69 all_files = os.listdir(path)70 files2 = []71 # 找到前缀格式文件72 if file_prefix:73 files = [file for file in all_files if file.startswith(file_prefix)]74 files2.extend(files)75 # 找到后缀格式文件76 if file_suffix:77 files = [file for file in all_files if file.endswith(file_suffix)]78 files2.extend(files)79 # 如果没有指定前缀或者后缀,就是删除全部文件80 if not file_prefix and not file_suffix:81 files2 = all_files82 # 开始删除文件83 for file in set(files2):84 file = os.path.join(path, file)85 os.remove(file)86 def remove_dir_and_files(self, path):87 """删除目录以及子目录子文件等"""88 # path=r'D:\a'89 if not os.path.exists(path):90 print('指定的目录不存在,不需要执行删除操作:' + path)91 # 删除92 shutil.rmtree(path)93 def remove_old_files(self, path, file_prefix, keep_days=5, datefmt='%Y-%m-%d.%H'):94 """删除旧的数据备份文件,要个要求文件命名格式: file_prefix_20150101 """95 # file_prefix = 'file_prefix_'96 # 判断路径是否存在97 if not os.path.exists(path):98 raise Exception('指定的路径不存在,请检查:%s' % path)99 # 找出指定前缀的文件100 files = [file for file in os.listdir(path) if file.startswith(file_prefix)]101 # 找到n天前的文件102 latest_day = (datetime.datetime.today() - datetime.timedelta(keep_days)).strftime(datefmt)103 old_file = file_prefix + latest_day104 remove_files = [file for file in files if file < old_file]105 # 开始删文件106 for file in remove_files:107 file = os.path.join(path, file)108 os.remove(file)109 print('删除旧的文件:%s' % file)110 return111 def back_file(self, file, suffix='_bak'):112 """备份文件"""113 file_bak = '%s%s' % (file, suffix)114 self.remove_files_if_exists(file_bak)115 shutil.copy(file, file_bak)116 def copy(self, source, target):117 """复制文件或文件夹到另外的地方"""118 if not os.path.exists(self.get_path_name(target)):119 os.makedirs(self.get_path_name(target))120 print('目标目录不存在,将创建:%s' % self.get_path_name(target))121 # 复制122 shutil.copyfile(source, target)123 def copy_dir(self, source, target):124 """复制文件夹"""125 if not os.path.exists(self.get_path_name(target)):126 os.makedirs(self.get_path_name(target))127 print('目标目录不存在,将创建:%s' % self.get_path_name(target))128 # 复制...

Full Screen

Full Screen

parallel_func.py

Source:parallel_func.py Github

copy

Full Screen

...112 pypickle.write(result_file, all_result_data)113 print('结果写到文件:%s'%result_file)114 # -------------------------------------------------------------------------------------------------------115 # 删除临时文件116 pyos.remove_files_if_exists(path, file_prefix=split_prefix)117 pyos.remove_files_if_exists(path, file_prefix=result_prefix)118 print('删除临时数据文件')119 t2 = datetime.datetime.now()120 print('完成全部并行计算,耗时:%d 秒'%(t2-t1).seconds)121 # 返回122 if result_file:123 return result_file124 else:125 return all_result_data126def _test():...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful