我想重命名Google驱动器文件夹中的100个文件。所以我写了下面的代码:
from google.colab import drive
drive.mount('/content/drive')
import os
for index in range(801,901):
src=f'/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/{index:04}X4.png'
dst=f'/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/x4/{index+200:04}X4.png'
os.rename(src,dst)
但是出现以下错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-24-313e46a40c1b> in <module>()
4 dst=f'/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/x4/{index+200:04}X4.png'
5 #print(dst)
----> 6 os.rename(src,dst)
FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0801X4.png' -> '/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/x4/1001X4.png'
我如何摆脱它?对于另一个文件夹,它工作正常。
答案 0 :(得分:0)
请查看错误“ FileNotFoundError”,该文件不存在。
仔细检查路径
我不知道colab / Google驱动器BUT的结构
尝试(路径中带有后缀“。”):
也
这是输出...我注意到我在第一行没有失败。检查您的路径。
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0801X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0802X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0803X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0804X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0805X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0806X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0807X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0808X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0809X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0810X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0811X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0812X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0813X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0814X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0815X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0816X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0817X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0818X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0819X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0820X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0821X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0822X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0823X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0824X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0825X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0826X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0827X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0828X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0829X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0830X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0831X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0832X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0833X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0834X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0835X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0836X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0837X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0838X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0839X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0840X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0841X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0842X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0843X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0844X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0845X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0846X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0847X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0848X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0849X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0850X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0851X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0852X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0853X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0854X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0855X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0856X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0857X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0858X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0859X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0860X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0861X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0862X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0863X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0864X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0865X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0866X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0867X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0868X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0869X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0870X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0871X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0872X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0873X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0874X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0875X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0876X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0877X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0878X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0879X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0880X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0881X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0882X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0883X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0884X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0885X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0886X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0887X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0888X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0889X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0890X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0891X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0892X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0893X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0894X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0895X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0896X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0897X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0898X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0899X4.png
/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4/0900X4.png
您可以尝试遍历文件夹中的每个png文件。
import os
for root, dirs, files in os.walk("/content/drive/My Drive/ai-projectroll-17198,17108/.div2k/images/DIV2K_valid_LR_bicubic/X4"):
for file in files:
if file.endswith(".png"): # add extra conditional logic here i.e if contains [801-901] etc etc
print(file) # get path
print(os.path.basename(file)) # get file name
os.rename(file, 'new file name') # rename the file