Click here to Skip to main content
15,843,591 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi New to this so sorry if I have it all wrong

I have this code that goes to drive c:\ then search all the folders in that drive for all jpg files that are biiger than 20kbytes then move them to a folder called test

All works fine BUT I want it to scan the c drive BUT from a named folder

This is the part that I cannot sort out

"
#%%
drive=win32api.GetLogicalDriveStrings().split('\000')[:-1] ## to select the drive
if drive[0]=='c:\\': # change here
    find_file(drive[0],re)


Hope some one can help
Kind Regards

What I have tried:

if drive[0]=='c:\\test\\': # change here


Faults
Posted
Updated 8-Aug-22 7:08am

The easiest way to do this would be to pass the starting path into the program at execution time. So your code would be something like:
Python
import sys
# Program begins here
search_path = 'C:\\' # default search path
if len(sys.argv) > 1:
    search_path = sys.argv[1] # get the path name from the command line

You can then start your program thus:
python searchapp.py C:\Users\master\Documents
 
Share this answer
 
Comments
build56 7-Aug-22 10:25am    
Sorry for my mistake in reply to this problem, did look for the reply but missed it.
Any way
problem I have a code in python that do this
1 look in the c drive then look in all folders
2 look for all jpg files bigger than 10kbytes (file Size)
3 move to a folder on d: drive called buiild
all works fine as is BUT

I want to be able to change from searching ALL folders to a folder called test or a folder that I can put in the code like c:\test here is the code
=================================
import os
import shutil
import re
import win32api
import glob
from PIL import Image
import PIL
from pathlib import Path
#%%
def find_file(root_folder, rex):
for root,dirs,files in os.walk(root_folder):
for filename in glob.glob(root+"/*.jpg", recursive=True):
try:
if os.stat(filename).st_size>10000:
result=filename#
print('Screenshot found')
directory = "buiild"
parent_directory= str(root_folder)
destination = os.path.join(parent_directory, directory)
source=os.path.join(root,filename)
print(source)
try:
os.makedirs(destination, exist_ok = True)
shutil.move(source,destination)
except OSError as error:
pass
else:
break
except PIL.UnidentifiedImageError:
pass
#%%
drive=win32api.GetLogicalDriveStrings().split('\000')[:-1] ## to select the drive
if drive[0]=='C:\\': # change here
find_file(drive[0],re)
=======================================
If you look at the line where it says
if drive[0]=='C:\\': # change here
I want to change that to something like
if drive[0]=='C:\\test': # change here
BUT IT DOES NTO WORK
Hope I have giving you more information this time
Thank you for you time
Richard MacCutchan 7-Aug-22 10:36am    
Then use the solution I already suggested to you. It is the simplest way to do what you want.
build56 7-Aug-22 14:13pm    
so do i paste your bit of code below the #% thentake out the other code about c drive etc?
Richard MacCutchan 7-Aug-22 14:27pm    
Yes, assuming that is where the program starts.
build56 7-Aug-22 15:41pm    
Hmmmm "program starts"
I thought from the above code that I pasted it would start at the top of the code from

import os

Or have I got that wrong
I have tidied up your code and removed all the redundant pieces, and it works fine in my tests.
Python
import shutil
import glob
import sys
import os

#%%
def find_file(root_folder):
    for root,dirs,files in os.walk(root_folder):
        for filename in glob.glob(root+"/*.jpg", recursive=True):    
            if os.stat(filename).st_size > 12000:
                print('Image file found:', filename)
                source=os.path.join(root,filename)
                print('source =', source)

                destination = os.path.join(root_folder, "build")
                print('destination =', destination)
                try:
                    print('moving ...')
                    os.makedirs(destination, exist_ok = True)
                    shutil.move(source,destination)
                except OSError as error:
                    pass
            else:
                continue
#%%

# Program begins here
search_path = 'C:\\' # default search path
if len(sys.argv) > 1:
    search_path = sys.argv[1] # get the path name from the command line
find_file(search_path)
 
Share this answer
 
Comments
build56 8-Aug-22 12:37pm    
WORKS GREAT
if I put in
py richard.py c:\
scans all drive c:\ and puts in the folder "build"in the Root of c:\ with all jpg files no problem
BUT if I put in
py richard.py c:\fred
It moves all jpg into c:\fred\build with all jpgs

That is no problem becaue I can look in there any way.
GREAT GREAT
But if a did just want to copy rarther the move is that a simple alteration in the scrip
I MUST thank you very much for all your time with me.
Is there anyway I can pay you for you time PLEASE
Richard MacCutchan 8-Aug-22 12:42pm    
Just change shutil.move(source,destination) to shutil.copy(source,destination). See shutil — High-level file operations — Python 3.10.6 documentation[^]. You should always try the documentation first.
build56 8-Aug-22 12:47pm    
Okay all done and working

PLEASE how can I pay you for ALL YOUR TIME?
Richard MacCutchan 8-Aug-22 12:51pm    
We do not accept payments. Everyone here (other than the wonderful staff) is a volunteer. We do it because we like to help people. Just remember in future when you are able, to do the same.
Richard MacCutchan 9-Aug-22 3:07am    
I forgot to mention that you pay on this site by clicking the "Accept Solution" link on the posted Solution. And you can also award stars (1 for poor and 5 for excellent) for any answers that you feel are appropriate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900