Python 3.6:join() 方法与os.path.join()的区别

一、函数作用:

join() :将序列、字符串 、元组等中的元素以指定的字符连接生成一个新的字符串。

os.path.join() : 将多个路径组合后返回

二、join()方法说明:

join()方法

语法:

str.join(sequence)

参数说明:

str:指定的字符,即分隔符

sequence:需要连接的元素

实例:

#字符串序列

seq = ("apple", "banana", "pear")

str = ""

print(str.join(seq))
applebananapear

str = " "

print(str.join(seq))
apple banana pear

str = "-"

print(str.join(seq))
apple-banana-pear

output:

applebananapear

三、os.path.join() 函数说明

os.path.join() 函数

语法:

os.path.join(path1[,path2[,……]])

实例:

import os 
path_root = 'F:\\data\\input'
dirs = os.listdir(path_root)

# 输出所有文件和文件夹
for file in dirs:
    path = os.path.join(path_root,file)
    path_test = os.path.join(path,'test')
    #print(path)
    print(path_test)

output:

F:\data\input\hamlet.txt
F:\data\input\hamlet2.txt
F:\data\input\input.txt
F:\data\input\test
F:\data\input\userurl_20150911.tdl
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()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值