主要是为了解决python脚本运行时,多个文件夹下的子文件夹相互调用时报错的问题。
from pathlib import Path
import shutil
destination_path = "/www/wwwroot/pythonEnv/pyETL/lib/python3.9/site-packages/ali_bigdata_myapplications.pth"
local_path = "/www/wwwroot/PythonProject/MyUtils/AddPath2SitPackages/ali_bigdata_myapplications.pth"
local_path = Path(local_path)
if local_path.exists():
Path(local_path).unlink()
def get_all_dir(pth, dir_list):
path = Path(pth)
for path_single in path.iterdir():
# 递归查询指定路径下所有目录
if path_single.is_dir():
dir_list.append(str(Path(path_single).absolute()))
get_all_dir(path_single, dir_list)
return dir_list
local_path.touch()
dir_list = []
with open(local_path, "a+") as f:
root_path = str(Path(local_path).parent.parent.parent)
f.write(root_path + "\n")
dir_list = get_all_dir(root_path, dir_list)
print(dir_list)
for path_single in dir_list:
f.write(str(Path(path_single).absolute()) + "\n")
with open(local_path, "r") as f:
print(f.readlines())
shutil.copyfile(local_path, destination_path)