'WinError2 when renaming a file
This code extracts pictures from a .xlsx file, copy them three times and renames them. It all works fine when I extract them to the same directory where the file and the code are, but if I try to extract it to a different directory (\renamedPix) I get:
FileNotFoundError: [WinError 2] Impossible to find the specified file: 'image1.jpeg' -> 'newPicone0.jpeg'
What I'm doing wrong? (Here below is an updated version: I tried using os.mkdir() but I get the same error)>
from zipfile import ZipFile
import re
import os
import shutil
file_name= "fote.xlsx"
directory= "renamedPix"
parent_dir = "C:\\Users\\divel\\Desktop"
path = os.path.join(parent_dir, directory)
os.mkdir(path)
with ZipFile(file_name, 'r') as zipObj:
counter = 0
for file in zipObj.infolist():
name = file.filename
match = re.findall("jpeg$", name)
if match:
filename = os.path.basename(file.filename)
for i in range(3):
source = zipObj.open(file)
target = open(os.path.join(path, filename), 'wb')
with source, target :
shutil.copyfileobj(source, target)
pic_names = ['one', 'two', 'three']
new_name = 'newPic'+pic_names[i]+str(counter)+'.jpeg'
os.rename(filename, new_name)
counter += 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|