strip 命令有效 "too much" 它甚至可以擦除不应擦除的内容

The strip command works "too much" it even erases what is not supposed to be erased

我正在尝试使用 the.strip() 函数删除多个变量中的部分文本。

这是我的代码:

image_list = [] # on créer un tableau dans lequel on va mettre toutes les images 
for filename in glob.glob(chemin+"\*.jpg" ): # I browse all.jpg files that are in the directory indicated by the variable "chemin".
   im=Image.open(filename)
   image_list.append(im) # I add the image to a table
   print("\n",filename, "\n", chemin,"\n", filename.strip(chemin),"\n\n") # Here is the test I do to see the different texts and the text where I use the strip function

这就是我得到的:

D:\Prog&Job\Ebay\Produit\Beyblade\toupie + boitier_bleu\images secondaire\boitier_8.JPG

D:\Prog&Job\Ebay\Produit\Beyblade\toupie + boitier_bleu\images 二级

8.JPG

首先是文件名和路径,然后是目录名,然后是路径,当我尝试剥离文件以删除所有路径时,它删除了开头 (此处:D:\Prog&Job\Ebay\Produit\Beyblade\toupie + boitier_bleu\images secondaire ),但它也删除了 "boitier_"

你知道为什么吗? 提前谢谢你

编辑:这是我想要的解决方案:

>>> import os
>>> full_path = '/book/html/wa/foo/bar/'
>>> print os.path.relpath(full_path, '/book/html')
'wa/foo/bar'

这是一个例子:How to remove a path prefix in python?

strip 不会删除找到的某个子字符串 prefix/suffix。它会删除在传递给它的字符串中找到的位于字符串开头或结尾的 任何字符 。注:

"TESTED SETS".strip("TES") # => "D " (T, E and S removed from front and back)

您可以通过多种方式删除前缀子字符串。这是一个:

if string.startswith(prefix):
    string = string[len(prefix):]