def create_file(file_path): if not os.path.exists(file_path): os.makedirs(file_path)
时间: 2024-04-28 10:26:12 浏览: 15
您的第一个问题是一个 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())
```
帮我重写如下python代码,#coding=utf-8-sig import re import os import xlrd import xlwt import time import datetime import math #import pandas import sys import shutil #通用部分 def time_mc(number_t): localTime = time.strftime("%Y%m%d%H%M%S", time.localtime()) time_mc_n
当然可以,以下是重写后的代码:
```python
#coding=utf-8-sig
import re
import os
import xlrd
import xlwt
import time
import datetime
import math
import sys
import shutil
# 通用部分
def time_mc(number_t):
localTime = time.strftime("%Y%m%d%H%M%S", time.localtime())
time_mc_n = localTime + str(number_t)
return time_mc_n
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def del_file(path):
if os.path.exists(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def get_file_path(root_path, file_type):
file_list = []
for dirpath, dirnames, filenames in os.walk(root_path):
for filename in filenames:
if filename.endswith(file_type):
file_list.append(os.path.join(dirpath, filename))
return file_list
# 主程序
if __name__ == '__main__':
pass
```
我将原来的代码中的注释进行了调整,并且将一些通用函数进行了函数封装,以方便代码的复用性和可读性。