Try to copy all jpg and jpeg files from C:\ drive to backup directory on desktop. The jpg and jpeg files should be equal or greater than 50 KB.
But I got this error:
return os.stat(filename).st_size
FileNotFoundError: [WinError 2] The system cannot find the file specified:
import os
import shutil
#Create Directory if don't exist in Desktop path
dir_name = "Backup"
dir_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
#dir_path = os.path.expanduser("~/Desktop")
file_path = os.path.join(dir_path, dir_name)
if not os.path.exists(file_path):
os.mkdir(file_path)
print(file_path)
try:
path = r'C:\\'
extensions = [".jpg", ".jpeg"]
for root, dir, files in os.walk(path):
for file in files:
if file.endswith(extensions[0]) or file.endswith(extensions[1]):
file_size = os.path.getsize(file)
if file_size >= 50:
print('File size:', file_size, 'KB')
shutil.copy2(os.path.join(root,file), file_path)
print(f"File==> {file}")
except KeyboardInterrupt:
print("Quite program!!!")
time.sleep(5)
os._exit(0)