【发布时间】:2020-01-18 04:34:01
【问题描述】:
我正在尝试使用 .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 secondaire
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'
【问题讨论】: