Python “剥离”命令起作用;“太多”;它甚至删除了不应该删除的内容

Python “剥离”命令起作用;“太多”;它甚至删除了不应该删除的内容,python,strip,Python,Strip,我试图使用.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".

我试图使用.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\u bleu\images secondaire\boitier\u 8.JPG

D:\Prog&Job\Ebay\Produit\Beyblade\toupie+boitier\u bleu\images secondaire

8.JPG

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

你知道为什么吗? 先谢谢你

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

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

这是一个例子:

strip
不删除作为前缀/后缀的特定子字符串。它删除传递给它的字符串中位于字符串开头或结尾的所有字符。注:

"TESTED SETS".strip("TES") # => "D " (T, E and S removed from front and back)
可以通过多种方式删除前缀子字符串。这是一个:

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

那是,不是。请小心标签。另外,Python对缩进很敏感,这使得代码不正确。我已经尝试过了,但是str对象没有startwith属性,是否需要导入模块才能工作?你的解释使我走上了正确的轨道,我没有很好地使用strip函数,但是我可以在sitestr对象上找到帮助,它肯定有
startswith
属性,但没有
startwith
属性。请注意@Amadan代码与注释中的额外“s”。