python中列表轉爲路徑

python中string也可以看做list,並調用list的方法,但是一旦調用原本的string就會變爲[ .. , .. , ..]的格式,要想再拼接爲string的形式就需要用for進行拼接,當然這個過程中也可以做很多的事情,比如路徑修改。

out_path = 'input_file=/home/xl/project/ChineseNER/tensorflow/test1.txt'

split_list = output_path.split('/')

# 去掉最後的test1.txt
file_name = split_list.pop()

head = ''
# 重新拼接路徑
for path in split_list:
    head = os.path.join(head, path)

# 拼接路徑並將最後的文件名替換
entity_file = '/' + head + '/entity' + '_' + file_name

print(entity_file)
# /home/xl/project/ChineseNER/tensorflow/entity_test1.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章