Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何用不同的字符将文件名从路径中分离出来?_Python - Fatal编程技术网

在python中,如何用不同的字符将文件名从路径中分离出来?

在python中,如何用不同的字符将文件名从路径中分离出来?,python,Python,在那里: 我使用glob.glob()函数得到了绝对路径+文件名的列表。然而,我尝试使用split函数来提取我的文件名,但我找不到最好的方法。例如: 我列表中的第一项是:'C:\Users\xxxxx\Desktop\Python Training\Python Training Home\Practices\Image Assembly\bird\u 01.png'。有几个像bird_02.png…等的图片。我成功地调整了它们的大小,并尝试将它们重新保存到不同的位置。请参阅下面的部分代码。我的

在那里:

我使用glob.glob()函数得到了绝对路径+文件名的列表。然而,我尝试使用split函数来提取我的文件名,但我找不到最好的方法。例如:

我列表中的第一项是:'C:\Users\xxxxx\Desktop\Python Training\Python Training Home\Practices\Image Assembly\bird\u 01.png'。有几个像bird_02.png…等的图片。我成功地调整了它们的大小,并尝试将它们重新保存到不同的位置。请参阅下面的部分代码。我的问题是我找不到在代码中提取“image\u filename”的方法。我只需要像伯德01,伯德02…等等。请帮帮我。先谢谢你

if not os.path.exists(output_dir):
os.mkdir(output_dir)
for image_file in all_image_files:
    print 'Processing', image_file, '...'
    img = Image.open(image_file)
    width, height =  img.size
    percent = float((float(basewidth)/float(width)))
    hresize = int(float(height) * float(percent))
    if width != basewidth:
        img = img.resize((basewidth, hresize), Image.ANTIALIAS) 
    image_filename = image_file.split('_')[0]
    image_filename = output_dir + '/' + image_filename
    print 'Save to ' + image_filename
    img.save(image_filename) 

您可以使用
os.path.split
函数提取路径的最后一部分:

>>> import os
>>> _, tail = os.path.split("/tmp/d/file.dat") 
>>> tail
'file.dat'
如果您只需要不带扩展名的文件名,一种安全的方法是使用
os.path.splitext

>>> os.path.splitext(tail)[0]
'file'

您可以使用
os.path.split
函数提取路径的最后一部分:

>>> import os
>>> _, tail = os.path.split("/tmp/d/file.dat") 
>>> tail
'file.dat'
如果您只需要不带扩展名的文件名,一种安全的方法是使用
os.path.splitext

>>> os.path.splitext(tail)[0]
'file'

要提取文件名(不含目录),请使用
os.path.basename()

在您的情况下,您可能还希望使用
rsplit
而不是
split
,并将拆分限制为一次:

>>> name = 'this_name_01.png'
>>> name.split('_')
['this', 'name', '01.png']
>>> name.rsplit('_', 1)
['this_name', '01.png']

要提取文件名(不含目录),请使用
os.path.basename()

在您的情况下,您可能还希望使用
rsplit
而不是
split
,并将拆分限制为一次:

>>> name = 'this_name_01.png'
>>> name.split('_')
['this', 'name', '01.png']
>>> name.rsplit('_', 1)
['this_name', '01.png']

有很多图像处理代码与问题无关,使问题更难理解。请尽量简明扼要地说明您的问题:)有许多图像处理代码与问题无关,使问题更难理解。对你的问题尽量简明扼要:)