簡體   English   中英

如何生成特定的目錄路徑?

[英]How to generate a specific directory path?

我正在處理目錄路徑問題,在這里我想生成一個我喜歡的特定目錄路徑mkdir -p root/PnG/bd_proc01/dataprep/JY2018/JD331/uuid -t/我想在其中獲取UUID的地方在外殼中填充了生成的“ UUID -t”,這是基於時間的UUID。 創建該路徑后,該路徑應存儲在變量中。

我還想要一個腳本,該腳本根據輸入的UUID查找特定的完整路徑。

誰能幫我這個忙嗎?

Thx,庫瑪

bash

在GNU / Linux中使用uuidgen (與uuid-runtime ):

dir_path="root/PnG/bd_proc01/dataprep/JY2018/JD331/"$(uuidgen -t)"/"
mkdir -p "$dir_path" || unset dir_path

將所需的路徑保存在變量dir_path (使用命令替換- $(uuidgen -t)獲得基於時間的UUID),創建目錄; 如果目錄創建失敗,請unset變量。 可以想象,對於成功創建目錄的情況,您將在變量dir_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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM