【发布时间】:2021-06-03 19:54:26
【问题描述】:
输入文件路径
示例:“/A/B/C/D/E/F”
-
如果不存在则检查路径“/A/B/C/D/E/F”是否存在
-
从“/A/B/C/D/E/F”中删除“/F”并检查system_file_paths中的“/A/B/C/D/E/”
一个。如果存在,则创建 F 路径
b.返回 F_ID
c。如果“/A/B/C/D/E”不存在,则进行下一步
-
从“/A/B/C/D/E/F”中删除 /E/F 并检查 system_file_paths 中存在的“/A/B/C/D”
一个。如果存在,创建 E/F
b.返回 F_ID
c。如果“/A/B/C/D”不存在,则进行下一步
-
从“/A/B/C/D/E/F”中删除 /D/E/F 并检查 system_file_paths 列表中存在的“/A/B/C”
一个。如果存在,则创建 D/E/F
b.返回 F_ID
c。如果“/A/B/C”不存在,则进行下一步
做,直到最后一步A,如果不存在,
我已经编写了代码,它按预期工作,但是性能正在降级,因为我有大量文件,例如 1000 的millons 和 file_path 长度
注意:我不是要在操作系统中创建任何目录,而是在应用程序数据库中更新
我的代码:
# Random system generation input paths for testing
import uuid
def file_path_list(num):
file_path_list = []
temp_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
for i in range(num):
temp = "{0}".format(i)
for j in temp_list:
temp += "{0}".format(j)
file_path_list.append(temp)
temp_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h','1','2','3','4']
for i in range(num):
temp = "{0}".format(i)
for j in temp_list:
temp += "{0}".format(j)
file_path_list.append(temp)
temp_list = ['a', 'b', 'c', 'd', 'x', 'y', 'z']
for i in range(num):
temp = "{0}".format(i)
for j in temp_list:
temp += "{0}".format(j)
file_path_list.append(temp)
return file_path_list
path_trees = file_path_list(10)
system_file_paths = {}
for path in path_trees:
system_file_paths[path] = str(uuid.uuid4())
# ************ system paths generation completed for testing ***************
# Input file
file_path = 'A/B/C/D/E/F'
def update_db(path_name):
_id = str(uuid.uuid4())
system_file_paths[path_name] = _id
return _id
# checking File path exist
def check_file_path_exists(file_path):
if file_path in system_file_paths:
return True
return False
# Looking solution, to speed up process performance, by applying any suitable algorithm and reduces multiple checks, in this method
def process_to_update_db(file_path):
# Split file path
file_path_split = file_path.split("/")
# Iterate loop
for reverse_path_indent in range(len(file_path_split) -1 , -1, -1):
# If iterate until last value, create root path
if reverse_path_indent == 0:
if not check_file_path_exists(file_path_split[0]):
_id = update_db(file_path_split[0])
for path_indent in range(1, len(file_path_split)):
file_path = "/".join(file_path_split[0: path_indent + 1])
_id = update_db(file_path)
return _id
#_id = update_db(file_path_split[0])
file_path_check = "/".join(file_path_split[0: reverse_path_indent])
if not check_file_path_exists(file_path_check):
continue
else:
for path_indent in range(reverse_path_indent, len(file_path_split)):
file_path = "/".join(file_path_split[0: path_indent + 1])
_id = update_db(file_path)
return _id
if not check_file_path_exists(file_path):
_id = process_to_update_db(file_path)
print(system_file_paths)
# Cross Verify path is created
if check_file_path_exists(file_path):
print(0)
else:
print(1)
【问题讨论】:
-
不是 python,但可能有帮助:在 linux 中,命令
mkdir -p /A/B/C/D/E/F完全符合您的要求。创建所有父目录(如果需要)。 -
是的,但我没有使用操作系统,在应用程序中我们需要实现类似的东西
-
您可以通过
os.makedirs调用实现@MrSmith42的建议。
标签: python python-3.x algorithm sorting