python os.mkdir -p
时间: 2023-05-18 16:04:07 浏览: 342
这是一个用于创建目录的 Python 命令,其中 -p 参数表示如果目录已经存在,则不会报错,而是直接跳过。例如,如果你想要创建一个名为 "test" 的目录,你可以使用以下命令:
```python
import os
os.makedirs("test", exist_ok=True)
```
这将会在当前工作目录下创建一个名为 "test" 的目录,如果该目录已经存在,则不会报错。
相关问题
python os.mkdir -pv
这个命令可以在 Python 中创建一个目录,如果该目录已经存在,则不会报错。其中 -p 参数表示递归创建目录,-v 参数表示在创建目录时显示详细信息。例如,如果要在当前目录下创建一个名为 test 的目录,可以使用以下代码:
import os
os.mkdir('test')
请将下列shell语句转换为python语句:if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then # Make train dict echo "Make a dictionary" mkdir -p $(dirname $dict) echo "<blank> 0" > ${dict} # 0 will be used for "blank" in CTC echo "<unk> 1" >> ${dict} # <unk> must be 1 if [ "${token_type}" = bpe ]; then # we borrowed these code and scripts which are related bpe from ESPnet. cut -f 2- -d" " data/${train_set}/text | sort > data/lang_char/input.txt tools/spm_train --input=data/lang_char/input.txt --vocab_size=${nbpe} --model_type=${bpemode} --model_prefix=${bpemodel} --input_sentence_size=100000000 tools/spm_encode --model=${bpemodel}.model --output_format=piece < data/lang_char/input.txt | tr ' ' '\n' | sort | uniq | awk '{print $0 " " NR+1}' >> ${dict} elif [ "${token_type}" = char ]; then tools/text2token.py -s 1 -n 1 data/${train_set}/text | cut -f 2- -d" " | tr " " "\n" \ | sort | uniq | grep -a -v -e '^\s*$' | awk '{print $0 " " NR+1}' >> ${dict} fi num_token=$(cat $dict | wc -l) echo "<sos/eos> $num_token" >> $dict # <eos> fi
以下是将上述shell语句转换为Python语句的代码:
```python
if stage <= 2 and stop_stage >= 2:
# Make train dict
print("Make a dictionary")
os.makedirs(os.path.dirname(dict), exist_ok=True)
with open(dict, "w") as f:
f.write("<blank> 0\n") # 0 will be used for "blank" in CTC
f.write("<unk> 1\n") # <unk> must be 1
if token_type == "bpe":
# we borrowed these code and scripts which are related bpe from ESPnet.
with open(f"data/{train_set}/text") as text_file:
texts = [line.strip().split()[1:] for line in text_file]
with open("data/lang_char/input.txt", "w") as input_file:
for text in texts:
input_file.write(" ".join(text) + "\n")
subprocess.run(f"tools/spm_train --input=data/lang_char/input.txt --vocab_size={nbpe} --model_type={bpemode} --model_prefix={bpemodel} --input_sentence_size=100000000", shell=True)
with open(f"{bpemodel}.vocab") as vocab_file:
vocab = [line.strip().split()[0] for line in vocab_file]
with open(dict, "a") as dict_file:
for i, vocab_item in enumerate(vocab):
dict_file.write(f"{vocab_item} {i+2}\n")
elif token_type == "char":
with open(f"data/{train_set}/text") as text_file:
texts = [line.strip().split()[1] for line in text_file]
texts = " ".join(texts)
tokens = subprocess.check_output(f"echo '{texts}' | tools/spm_encode --model={bpemodel}.model --output_format=piece", shell=True)
tokens = tokens.decode().split("\n")
tokens = [token.strip() for token in tokens if token.strip() != ""]
tokens = sorted(set(tokens))
with open(dict, "a") as dict_file:
for i, token in enumerate(tokens):
dict_file.write(f"{token} {i+2}\n")
num_token = sum(1 for line in open(dict))
with open(dict, "a") as dict_file:
dict_file.write(f"<sos/eos> {num_token}\n") # <eos>
```
需要注意的是,在Python中也需要使用相应的模块和工具来实现shell语句中的一些操作,例如在此代码中使用了`os`、`subprocess`模块和`tools/spm_train`、`tools/spm_encode`脚本来进行文件操作和调用外部命令。另外,由于Python中没有直接对应的`$`符号,需要使用`f-string`或者`str.format()`方法来进行字符串格式化。
阅读全文