Windows
import wmi
from pathlib import Path
import win32file
import psutil
@xxx.route("/getPath", methods=["GET"])
def getPath():
try:
get_data = request.args.to_dict() #替换相应的文件路径即可
getPath = get_data.get("path","")
if getPath == "":
list1 = psutil.disk_partitions()
list2 = []
for i in list1:
list2.append(i[0])
return {'code': '200', 'msg': '返回正常', "data": list2} # 默认返回盘符
else:
path = Path(getPath)
list1 = []
for i in path.iterdir():
if (not i.is_file()):
flag = win32file.GetFileAttributesW(str(i))
if flag & 2 == 0:
list1.append(str(i))
return {'code': '200', 'msg': '返回正常','data':list1}
except Exception as e:
return {'code': '201', 'msg': '文件无法访问'}
Linux
import os
getPath = 'D:/project/pythonTest/ceshi'
list1 = os.listdir(getPath)
print(list1)