Find if a file existing within 1000s of folder/sub-folder - each file has a unique presence

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu May 21 04:07:13 EDT 2015


On Thursday 21 May 2015 15:34, chaotic.sid at gmail.com wrote:

> So I was trying to dir /s /b using python.
> Now since the file's path name is computed using other part of the code, I
> am feeding in a variable here and somehow it does not seem to work.. :(
> 
> Intent is to run following command from python
> dir /s /b "c:/abc/def/ghjmain\features\XYZ\*<filename>"

I don't use Windows and have no idea what the /s and /b flags do, but 
something like this should work:

import glob
print(glob.glob("c:/abc/def/ghjmain/features/XYZ/*<filename>"))


Don't use backslashes \ as they have special meaning to Python. Use forward 
slashes and let Python convert them as needed.

Also, this only looks in the C:.../XYZ directory. I think that Python 3.4 or 
better will accept a ** wildcard instead of * to look in subdirectories too. 
Or you can use a simple directory walker:

# Untested.
import os, os.path
start = "c:/abc/def/ghjmain/features/XYZ/"
for dirpath, dirnames, filenames in os.walk(start):
    for name in filenames:
        if name.endswith("filename"):
            print(os.path.join(dirpath, name))


If you can do the file listing from Python without calling out to an 
external process, it will likely be faster.



> My present code.
> import subprocess
> 
> for row in range(1,max_row):
>     tempID_cell = "C" + str(row);
>     tempID = ws[tempID_cell].value;
>     print (tempID);
>     Command = "c:\\abc\\def\\ghj\\main\\features\\XYZ\\*" + str(tempID) +
>     ".cli"; print (Command);
>     IsPresent = subprocess.check_output("dir /s /b Command", shell=True);
> 
> Any help would really be appreciated.


Start by defining "somehow it does not seem to work" (your words, above). 
What happens? Does your computer crash? It downloads files off the Internet? 
Raises an exception? Prints the wrong information?

If it raises an exception, you should COPY AND PASTE the full traceback. If 
it prints an error message from dir, you should do the same.

What happens when you take the command you generate and run it in the shell?

I think what you want is:

path = "c:/abc/def/ghj/main/features/XYZ/*" + str(tempID) + ".cli"
command = "dir /s /b " + path
output = subprocess.check_output(command, shell=True)

Actually, I think you don't need the shell argument. Try this instead:

path = "c:/abc/def/ghj/main/features/XYZ/*" + str(tempID) + ".cli"
output = subprocess.check_output(['dir', '/s', '/b', path])

That is likely to be safer and will help avoid shell injection attacks if 
the path comes from an untrusted source. Any subprocess experts want to 
confirm this?



P.S. Python is not C or Javascript. There is no need to end each line with a 
semicolon. That just makes you seem like a n00b.


-- 
Steve




More information about the Python-list mailing list