Python 如何生成特定的目录路径?

Python 如何生成特定的目录路径?,python,bash,shell,Python,Bash,Shell,我正在研究目录路径问题,我想生成一个特定的目录路径,我希望它像mkdir-p root/PnG/bd_proc01/dataprep/JY2018/JD331/uuid-t/一样,我想在uuid位置填充生成的“uuid-t”,它是shell中基于时间的uuid。一旦创建了该路径,该路径就应该存储在一个变量中 我还需要一个脚本,根据作为输入提供的UUID查找特定的完整路径 有人能帮我吗 Thx, 库马尔bash: 在GNU/Linux中使用uuidgen(随uuid运行时提供): dir_path

我正在研究目录路径问题,我想生成一个特定的目录路径,我希望它像
mkdir-p root/PnG/bd_proc01/dataprep/JY2018/JD331/uuid-t/
一样,我想在uuid位置填充生成的“uuid-t”,它是shell中基于时间的uuid。一旦创建了该路径,该路径就应该存储在一个变量中

我还需要一个脚本,根据作为输入提供的UUID查找特定的完整路径

有人能帮我吗

Thx,
库马尔

bash

在GNU/Linux中使用
uuidgen
(随
uuid运行时提供)

dir_path="root/PnG/bd_proc01/dataprep/JY2018/JD331/"$(uuidgen -t)"/"
mkdir -p "$dir_path" || unset dir_path
在变量
dir\u path
中保存所需的路径(使用命令替换获取基于时间的UUID-
$(uuidgen-t)
),创建目录;如果目录创建失败,
取消设置该变量。可以想象,对于成功的目录创建案例,您将在变量
dir\u path
中获得目录名


python

使用
uuid
,与
bash
类似的逻辑:

import os 
import uuid
# `uuid.uuid1()` creates time (and host) based UUID
dir_path = os.path.join('root/PnG/bd_proc01/dataprep/JY2018/JD331/', \
                          '{}'.format(str(uuid.uuid1())))
try:
    # Recursive directory creation (like `mkdir -p`)
    os.makedirs(dir_path)
# You can be precise here rather than catching the whole Exception class
except Exception: 
    del dir_path

bash

在GNU/Linux中使用
uuidgen
(随
uuid运行时提供)

dir_path="root/PnG/bd_proc01/dataprep/JY2018/JD331/"$(uuidgen -t)"/"
mkdir -p "$dir_path" || unset dir_path
在变量
dir\u path
中保存所需的路径(使用命令替换获取基于时间的UUID-
$(uuidgen-t)
),创建目录;如果目录创建失败,
取消设置该变量。可以想象,对于成功的目录创建案例,您将在变量
dir\u path
中获得目录名


python

使用
uuid
,与
bash
类似的逻辑:

import os 
import uuid
# `uuid.uuid1()` creates time (and host) based UUID
dir_path = os.path.join('root/PnG/bd_proc01/dataprep/JY2018/JD331/', \
                          '{}'.format(str(uuid.uuid1())))
try:
    # Recursive directory creation (like `mkdir -p`)
    os.makedirs(dir_path)
# You can be precise here rather than catching the whole Exception class
except Exception: 
    del dir_path

比如:
path='//some//long//%s-t'%generated\u uuid
?将
uuid-t
放在shell脚本的backticks中:
mkdir-p root/PnG/bd\u proc01/dataprep/JY2018/JD331/`uuid-t`///code>比如:
path='//some//long//%s-t'%generated\u uuid
?将
放在backticks中您的shell脚本:
mkdir-p root/PnG/bd_proc01/dataprep/JY2018/JD331/`uuid-t`/
您还可以建议如何获取JY2018和JD331吗automatically@Arun你打算如何获得这些价值观?在你的问题中没有关于这方面的任何信息。我希望在执行脚本时自动获取这些值,并且该值应该像目录路径中的JY(JobYear)+CurrentYear和JD(JobDay)+dayofYear,这样我就不必每次都在路径中给出这些值。对于这个模糊的问题,我很抱歉。我试过了,但不满意。
today=date.today()currentday=('JD'+str(today.day))currentyear=('JY'+str(today.year))\uuid.uuid1()创建基于时间(和主机)的uuid dir\u path=os.path.join('root/PnG/bd\u proc01/dataprep/JY+'(today.year)/'JD'+(today.day)/“{}.格式(str(uuid.uuid1())
@Arun这是不一样的。请问一个新问题。您能否建议如何获取JY2018和JD331automatically@Arun您打算如何获取这些值?您的问题中没有任何关于这些值的信息。我希望在执行脚本时自动获取这些值,并且该值应类似于JY(JobYear)+CurrentYear和JD(JobDay)+dayofYear在该目录路径中,这样我就不必每次都在路径中给出这些值。对于这个模糊的问题,我很抱歉。我尝试了这个方法,但不满意。
today=date.today()currentday=('JD'+str(today.day))CurrentYear=('JY'+str(today.year))#uuid.uuid1()创建时间(和主机)基于UUID dir_path=os.path.join('root/PnG/bd_proc01/dataprep/JY++(today.year)/'JD++(today.day)/\'{}.format(str(UUID.uuid1()))
@Arun这是不同的。请问一个新问题。