def create_file(file_path): if not os.path.exists(file_path): os.makedirs(file_path)
时间: 2024-04-28 20:26:12 浏览: 187
您的第一个问题是一个 Python 代码段,这段代码的作用是什么?
这段代码实现的功能是创建一个文件夹(如果该文件夹不存在的话),其输入参数为文件夹的路径。代码首先检查该路径所指的文件夹是否存在,如果不存在则使用 os.makedirs() 方法创建该文件夹。这个方法可以创建多级目录,如果存在相同的目录则不会报错。
相关问题
@app.route('/restruct', methods=['POST']) def restruct(): dch = request.form['dch'] dch_folder = "restr" + dch count = 1 while os.path.exists(os.path.join('D:\cj', dch_folder)): dch_folder = "restr" + dch + "_" + str(count) count += 1 os.makedirs(os.path.join('D:\cj', dch_folder, 'photos')) os.makedirs(os.path.join('D:\cj', dch_folder, 'kmls')) os.makedirs(os.path.join('D:\cj', dch_folder, 'project')) photos_folder = os.path.join('D:\cj', dch_folder, 'photos') kmls_folder = os.path.join('D:\cj', dch_folder, 'kmls') project_folder = os.path.join('D:\cj', dch_folder, 'project') for file in request.files.getlist('files'): filename = file.filename file.save(os.path.join(photos_folder, filename)) dhs = request.form.getlist('dh') for dh in dhs: src_path = os.path.join(r'D:/911kml/', dh + '.xlsx.kml') dst_path = os.path.join(r'D:/cj', dch_folder, 'kmls/', dh + '.xlsx.kml') shutil.copy(src_path, dst_path) subprocess.Popen(["python", "restruct.py", dch, photos_folder, project_folder, kmls_folder])使用python3.6版本的websocket,将这段代码运行中的输出信息,实时发送给前端,给出server.py和client.py
server.py:
```python
import asyncio
import websockets
import subprocess
async def echo(websocket, path):
dch = await websocket.recv()
dch_folder = "restr" + dch
count = 1
while os.path.exists(os.path.join('D:\cj', dch_folder)):
dch_folder = "restr" + dch + "_" + str(count)
count += 1
os.makedirs(os.path.join('D:\cj', dch_folder, 'photos'))
os.makedirs(os.path.join('D:\cj', dch_folder, 'kmls'))
os.makedirs(os.path.join('D:\cj', dch_folder, 'project'))
photos_folder = os.path.join('D:\cj', dch_folder, 'photos')
kmls_folder = os.path.join('D:\cj', dch_folder, 'kmls')
project_folder = os.path.join('D:\cj', dch_folder, 'project')
async for file in websocket:
filename = file.filename
file.save(os.path.join(photos_folder, filename))
dh = await websocket.recv()
src_path = os.path.join(r'D:/911kml/', dh + '.xlsx.kml')
dst_path = os.path.join(r'D:/cj', dch_folder, 'kmls/', dh + '.xlsx.kml')
shutil.copy(src_path, dst_path)
process = await asyncio.create_subprocess_exec(
"python", "restruct.py", dch, photos_folder, project_folder, kmls_folder,
stdout=asyncio.subprocess.PIPE
)
while True:
output = await process.stdout.readline()
if output == b'':
break
await websocket.send(output.decode())
async def main():
async with websockets.serve(echo, "localhost", 8765):
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
```
client.py:
```python
import asyncio
import websockets
import aiohttp
async def upload_files(dch, files, dh):
async with aiohttp.ClientSession() as session:
async with session.ws_connect('ws://localhost:8765') as ws:
await ws.send(dch)
for file in files:
await ws.send(file)
await ws.send(dh)
async for message in ws:
print(message)
async def main():
dch = 'example'
files = ['example1.jpg', 'example2.jpg']
dh = 'example'
await upload_files(dch, files, dh)
if __name__ == "__main__":
asyncio.run(main())
```
import jenkins import json import os from models.config_classes import * from models.ci_config import * from utils.utils import * from xml_re import * CREATE = "config" DELETE = "config_delete" # write config path xmlSaveDir = "./config/xml_get" # load jobs def load_jobs_list(server, configFile): if not os.path.exists(xmlSaveDir): os.makedirs(xmlSaveDir) # generate file by file with open(f'./config/{configFile}.json', 'r') as f: # default `r` mode globalConfig = json.load(f) # get basement config if configFile != DELETE : selected_job = globalConfig["basicJob"] jobConfigXml = server.get_job_config(selected_job) with open(f'./config/xml_get/{selected_job}.xml', 'w') as f: # default `r` mode f.write(jobConfigXml) # jobList jenkinsJobsConfigs = GlobalConfig.from_dict(globalConfig) jobList = ExclusiveConfigList.from_dict(jenkinsJobsConfigs) return jobList, jobConfigXml
这段代码是用 Python 语言编写的,主要是与 Jenkins 服务器进行交互,对其上的任务进行管理。具体实现了以下功能:
1. 导入了 Jenkins、json、os 等库;
2. 导入了自己写的一些模块,如 config_classes、ci_config 和 utils 等;
3. 定义了 CREATE 和 DELETE 两个字符串常量;
4. 定义了 xmlSaveDir 变量,表示保存任务配置文件的路径;
5. 实现了 load_jobs_list 函数,该函数用于加载 Jenkins 服务器中的任务列表,并将其转换成一个 ExclusiveConfigList 对象;
6. 在 load_jobs_list 函数中,使用 Jenkins 的 API 获取某个任务的配置信息,并将其保存到本地文件中。
总体来说,这段代码主要是用于对 Jenkins 服务器上的任务进行配置和管理的,包括获取任务列表、获取任务配置信息等。
阅读全文