Python 仅从文件名路径列表中提取maya文件中使用的纹理文件名
我有以下代码,用于列出Maya文件中使用的纹理文件。当我运行代码时,我会得到一个纹理文件列表及其路径。但我只需要一个文件名列表,我正在尝试提取它们,但没有得到我期望的列表Python 仅从文件名路径列表中提取maya文件中使用的纹理文件名,python,maya,Python,Maya,我有以下代码,用于列出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:
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
当我执行时,我会得到路径列表的相同列表。有什么问题吗 您将希望使用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)
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)
这将为您提供如下信息:
['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)
new_list.append(file_name)
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)
您需要使用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)
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)
这将为您提供如下信息:
['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)
new_list.append(file_name)
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)
谢谢,我有文件名。但我还是得到了这个名单,名字是[u'safaa_amir_clean_body_dif.jpg',u'safaa_amir_body_nrlMap.tif']。一开始我怎么去掉“u”呢?没关系。我不必费心把它扔掉,因为它没有任何区别。ThanksIt只是表示字符串项是unicode而不是ascii,它实际上并不影响列表,请尝试打印列表中的每个项,看看会发生什么。如果你真的想摆脱它,我想你可以在附加行中使用str(file_name/stripped_file_name),但可能不起作用。顺便说一句,不要使用'path'作为变量名,因为它是一个常见的模块。谢谢,我得到了文件名。但我还是得到了这个名单,名字是[u'safaa_amir_clean_body_dif.jpg',u'safaa_amir_body_nrlMap.tif']。一开始我怎么去掉“u”呢?没关系。我不必费心把它扔掉,因为它没有任何区别。ThanksIt只是表示字符串项是unicode而不是ascii,它实际上并不影响列表,请尝试打印列表中的每个项,看看会发生什么。如果你真的想摆脱它,我想你可以在附加行中使用str(file_name/stripped_file_name),但可能不起作用。顺便说一句,不要使用'path'作为变量名,因为它是一个普通模块。