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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章