我有以下代码列出我的Maya文件中使用的纹理文件。当我运行代码时,我得到一个纹理文件列表及其路径。但我只需要一个文件名列表,我正在尝试提取它们,但没有按照我的预期获得列表。
import maya.cmds as cmds
# Gets all 'file' nodes in maya
fileList = cmds.ls(type='file')
texture_filename_list = []
# For each file node..
for f in fileList:
# Get the name of the image attached to it
texture_filename = cmds.getAttr(f + '.fileTextureName')
texture_filename_list.append(texture_filename)
print texture_filename_list
The output is [u'D:/IRASProject/RVK/SAFAA/Shared/sourceimages/chars/amir/4k/safaa_amir_clean_body_dif.jpg', u'D:/IRASProject/RVK/SAFAA/Shared/sourceimages/chars/amir/4k/safaa_amir_body_nrlMap.tif', etc]
现在从这个列表中我只需要提取文件名,所以我将代码添加为
for path in texture_filename_list :
path.split('/')
path_list.append(path)
print path_list
当我执行时,我获得了与path_list相同的列表。可能是什么问题呢?
答案 0 :(得分:1)
您将需要使用os.path.basename(),如下所示:
import os
import os.path
new_list = []
for each in texture_filename_list:
file_name = os.path.basename(each)
new_list.append(file_name)
这会给你类似的东西:
['safaa_amir_clean_body_dif.jpg', 'safaa_amir_body_nrlMap.tif']
如果你想要没有扩展名,你可以改变它:
import os
import os.path
new_list = []
for each in texture_filename_list:
file_name = os.path.basename(each)
stripped_file_name = os.path.splitext(file_name)[0]
new_list.append(stripped_file_name)