如何 os.chdir 进入先前通过 os.path.join 在循环中创建的目录?
How to os.chdir into a directory previously made through os.path.join within loop?
我正在编写代码,为服务创建 grpc ssl 证书并将它们放置在通过扫描两个 json 文件创建的目录中。
此代码的一部分是遍历 json 并在一个循环中根据其中列出的服务名称创建目录。我想使用 chdir 函数 cd 进入最近在每次循环迭代中创建的目录,以便 运行 ssl 脚本函数,称为“cert_create”。
我会在下面列出示例代码
for data in servicesUsedJson:
pwd = os.getcwd()
service = data.get("service")
used = data.get("services_used")
if service is None or used is None:
raise ValueError("Missing information")
for u in used:
os.path.join(pwd, "certs", service, u)
# this is where I would like to add os.chdir into the directory created above in order to run the function below
cert_create(str(serviceIp[u])))
try:
path = os.path.join(pwd, "certs", service, u) # Save path to variable
if not os.path.exists(path): # Create directory if does not exist
os.mkdir(path)
os.chdir(path) # Change directory
except OSError:
print ("Failed.")
我正在编写代码,为服务创建 grpc ssl 证书并将它们放置在通过扫描两个 json 文件创建的目录中。
此代码的一部分是遍历 json 并在一个循环中根据其中列出的服务名称创建目录。我想使用 chdir 函数 cd 进入最近在每次循环迭代中创建的目录,以便 运行 ssl 脚本函数,称为“cert_create”。
我会在下面列出示例代码
for data in servicesUsedJson:
pwd = os.getcwd()
service = data.get("service")
used = data.get("services_used")
if service is None or used is None:
raise ValueError("Missing information")
for u in used:
os.path.join(pwd, "certs", service, u)
# this is where I would like to add os.chdir into the directory created above in order to run the function below
cert_create(str(serviceIp[u])))
try:
path = os.path.join(pwd, "certs", service, u) # Save path to variable
if not os.path.exists(path): # Create directory if does not exist
os.mkdir(path)
os.chdir(path) # Change directory
except OSError:
print ("Failed.")