【问题标题】:Overwrite prefixes on image files/folder覆盖图像文件/文件夹的前缀
【发布时间】:2020-05-12 13:48:26
【问题描述】:

有没有办法覆盖保存在桌面上的图像文件名。我想调整一些前缀。

例如。 在 s_pclr-01.jpg 之前 -> 我想要什么(之后)01.jpg

请看下面的代码。我创建了一个路径来提取具有 100 多个图像名称的文件夹,其中包含 s_pclr、pclr- 和 s_pclr。只是希望能够覆盖它们。

colors_path = os.listdir(path)
for n in colors_path: 
    d = n.replace('s_pclr-', '') #strong text
    f = n.replace('pclr-', '')
    e = n.replace('s_pclr_', '')
    print('{}, {}, {}'.format(d, f, e))
    os.rename(path, d)

运行后出现此错误:

FileNotFoundError: [WinError 3] 系统找不到指定的路径:'C:\Users\david.han\OneDrive - Barco Uniforms\Desktop\Colors'

【问题讨论】:

  • 您是否检查过您传递给listdir 方法的完整路径是否包含“Colors”目录?
  • 是的,它给了我这个。例如 s_pclr_ORT.jpg s_pclr_SAH.jpg s_pclr_SPO.jpg s_pclr_WIN.jpg s_pclr_WIR.jpg TAQ.jpg TAT.jpg TBB.jpg TBE.jpg TBL.jpg TBM.jpg TCE.jpg
  • 另一个问题可能是因为权限。对于您发布的示例目录列表,您是否使用了 Windows 上的 Window Manager 工具或其他 python 工具?
  • @NanaOwusu 我用的是jupyter笔记本

标签: python pandas windows


【解决方案1】:

您无法重命名 path,请尝试重命名实际文件:

colors_path = os.listdir(path)
for n in colors_path: 
    d = n.replace('s_pclr-', '') #strong text
    f = n.replace('pclr-', '')
    e = n.replace('s_pclr_', '')
    print('{}, {}, {}'.format(d, f, e))
    # assuming "n" to be the old name and "d" to be the new one
    os.rename( os.path.join( path, n), os.path.join( path, d))

【讨论】:

  • 非常感谢。不知道我必须使用 os.path.join 进行实际更改。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多