如何使用文件夹名称作为Python中的前缀重命名多个图像文件
我有一个名为“ cat”的文件夹,其中包含许多.jpg文件,例如069.jpg,208.jpg,1134.jpg,img_thermal_158.jpg等。
我想将它们重命名为CAT_069.JPG,CAT_208.JPG,CAT_1134.JPG,CAT_IMG_THERMAL_158.JPG等。
如何用Python实现这一目标?我正在使用Windows 10计算机上的Anaconda使用Jupyter笔记本来运行Python。
我对不起作用的尝试
import os
pre = "cat_"
[os.rename(f, pre + str(f)) for f in os.listdir('../Dataset/Train/Cat')
if f.endswith(".jpg")]
给出了这个错误,
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_7536\2602524164.py in <cell line: 4>()
2
3 pre = "cat_"
----> 4 [os.rename(f, 'cat_' + str(f)) for f in os.listdir('../Dataset/Train/Cat')
5 if (f.endswith(".jpg"))]
~\AppData\Local\Temp\ipykernel_7536\2602524164.py in <listcomp>(.0)
2
3 pre = "cat_"
----> 4 [os.rename(f, 'cat_' + str(f)) for f in os.listdir('../Dataset/Train/Cat')
5 if (f.endswith(".jpg"))]
FileNotFoundError: [WinError 2] The system cannot find the file specified: '069.jpg' -> 'cat_069.jpg'
我不知道这是什么问题,因为这绝对是正确的道路。我还尝试了绝对路径,“ c:\ users \ me \ jupiter_notebooks \ dataset \ train \ train \ cat”具有相同的错误。
[os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]
FileNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_7536\980149228.py in <cell line: 1>()
----> 1 [os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]
~\AppData\Local\Temp\ipykernel_7536\980149228.py in <listcomp>(.0)
----> 1 [os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]
FileNotFoundError: [WinError 2] The system cannot find the file specified: '069.jpg' -> 'cat_069.jpg'
我还尝试了下面产生类似错误的下面尝试。
import shutil
import os
newdir="C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\"
for f in (os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\')):
if f[-4:]==(".jpg"):
if os.path.isdir(newdir):
shutil.copy(f,newdir+"/"+"Cat_"+f)
else:
os.mkdir(newdir)
shutil.copy(f,newdir+"/"+"Cat_"+f)
有人可以
cwd = os.getcwd()
cwd
'C:\\Users\\me\\Jupiter_Notebooks\\Dataset'
# my default os.listdir() is
os.listdir()
['.ipynb_checkpoints',
'Test',
'Thermal_image_notebook.ipynb',
'Train'...
告诉我我需要在代码中更改什么?谢谢。
ps这是文件,
I have a folder called 'Cat' which contains lots of .jpg files, such as 069.jpg, 208.jpg, 1134.jpg, img_thermal_158.jpg and so on.
I want to rename these as cat_069.jpg, cat_208.jpg, cat_1134.jpg, cat_img_thermal_158.jpg and so on.
how can I achieve this with python? I'm using Jupyter notebooks via Anaconda on a Windows 10 machine to run python.
my attempt at this which does not work
import os
pre = "cat_"
[os.rename(f, pre + str(f)) for f in os.listdir('../Dataset/Train/Cat')
if f.endswith(".jpg")]
It gives this error,
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_7536\2602524164.py in <cell line: 4>()
2
3 pre = "cat_"
----> 4 [os.rename(f, 'cat_' + str(f)) for f in os.listdir('../Dataset/Train/Cat')
5 if (f.endswith(".jpg"))]
~\AppData\Local\Temp\ipykernel_7536\2602524164.py in <listcomp>(.0)
2
3 pre = "cat_"
----> 4 [os.rename(f, 'cat_' + str(f)) for f in os.listdir('../Dataset/Train/Cat')
5 if (f.endswith(".jpg"))]
FileNotFoundError: [WinError 2] The system cannot find the file specified: '069.jpg' -> 'cat_069.jpg'
I don't know what's wrong because it is definitely the right path. I have also tried the absolute path, "C:\Users\me\Jupiter_Notebooks\Dataset\Train\Cat" with the same error.
[os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]
FileNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_7536\980149228.py in <cell line: 1>()
----> 1 [os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]
~\AppData\Local\Temp\ipykernel_7536\980149228.py in <listcomp>(.0)
----> 1 [os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]
FileNotFoundError: [WinError 2] The system cannot find the file specified: '069.jpg' -> 'cat_069.jpg'
I have also tried below which produces a similar error.
import shutil
import os
newdir="C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\"
for f in (os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\')):
if f[-4:]==(".jpg"):
if os.path.isdir(newdir):
shutil.copy(f,newdir+"/"+"Cat_"+f)
else:
os.mkdir(newdir)
shutil.copy(f,newdir+"/"+"Cat_"+f)
it is
cwd = os.getcwd()
cwd
'C:\\Users\\me\\Jupiter_Notebooks\\Dataset'
# my default os.listdir() is
os.listdir()
['.ipynb_checkpoints',
'Test',
'Thermal_image_notebook.ipynb',
'Train'...
Can someone tell me what I need to change in my code please? thank you.
P.S. here are the files, https://github.com/bluetail14/Thermal_data_images_project
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,最简单的解决方案是将CWD更改为具有文件的目录,重命名文件并在必要时更改CWD:
The easiest solution, in my view, is to change the cwd to the directory that has the files, rename the files, and change the cwd back if necessary: