-1

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)
Pycoder
  • 16
  • 4
  • 1
    What exactly does not work? Can you please provide some more information except of yuor code? –  Oct 24 '21 at 14:58
  • 1
    Welcome to Stack Overflow. Please read [ask]. I see a statement of what you're trying to do and some code. Do you have a question? In what way does this code fail to achieve your goal? – ChrisGPT was on strike Oct 24 '21 at 14:58
  • @js-on I think this line not work: file_size = os.path.getsize(file) – Pycoder Oct 24 '21 at 15:04
  • 1
    I guess your problem is that you compare bytes with „kilobytes“, os.path.getsize return bytes and your if matches here against 50 bytes and not kilobytes ;) – droebi Oct 24 '21 at 15:30
  • 1
    https://docs.python.org/3/library/os.path.html -> os.path.getsize(path) Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible. – droebi Oct 24 '21 at 15:37
  • @droebi Thanks so much I changed 50 to 50000 it solvedv problem – Pycoder Oct 24 '21 at 15:37
  • @Pycoder you’re welcome – droebi Oct 24 '21 at 15:39

1 Answers1

1

Replace file_size = os.path.getsize(file) with

file_size = os.path.getsize(os.path.join(root,file))

Edit

import os
import shutil
import time

#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(os.path.join(root,file))
                                if file_size <= 50000:
                                        print('File size:', file_size, 'KB')

                                        shutil.copyfile(os.path.join(root,file), os.path.join(file_path,file))
                                        print(f"File==> {file}")


except KeyboardInterrupt:
    print("Quite program!!!")
    time.sleep(5)
    os._exit(0)
HamzaFarooq
  • 429
  • 1
  • 4
  • 16