0

I am currently trying to search for a list of missing attachments that are scattered throughout our file share server.I have a txt file with a list of filenames I would like to find. Ideally at some point I would like to copy the file to another location when found. At this moment it doesn't seem to be working, because it returns no results and I have verified the files exist.

import os

source = open("Test.txt", "r")
dest = '//somewhere/somefolder'
path = '//somewhere/anotherfolder'

for line in source:
   print 'Searching . . .'
   print line
     for root, dirs, files in os.walk(path):

       for name in files:

         if name in line:
            # shutil.copy(os.path.join(root, name), dest)
            print 'Found!'
            print path, name

print 'Done';
Tenso
  • 23
  • 6
  • wouldn't you also need to do `source.read()`? At least in Py 2 that is a thing – patrick Apr 25 '17 at 17:10
  • Please fix the indentation. As it currently stands the code doesn't even run, because there is an unexpected indent between `print line` and `for root, dirs, files …`. – mkrieger1 Apr 25 '17 at 17:10
  • @patrick no, `for x in some_file.read()` would iterate over characters in the file. `for x in some_file` iterates over the lines. – Adam Smith Apr 25 '17 at 17:17
  • 1
    @AdamSmith I stand corrected, thanks for pointing it out. Looks like my brain is still on vacation. – patrick Apr 25 '17 at 17:22
  • @PeterWood This won't cause the behavior here (`"foo" in "some line with a foo in it\n"` doesn't need to be stripped) – Adam Smith Apr 25 '17 at 17:26
  • So I actually got this to work removing the name.lower and just using name. – Tenso Apr 25 '17 at 17:30
  • If my list of filenames has the path included, is there a way to ignore the path and only search for the filename? (ex. //somewhere/anotherfolder/filename.pdf – Tenso Apr 25 '17 at 17:37

1 Answers1

0

Question/Comment: is there a way to ignore the path and only search for the filename?

For instance:

import os

fpath = "//somewhere/anotherfolder/filename.pdf"
fname = os.path.basename(fpath)

print('fname=%s' % fname)

Output:
fname=filename.pdf

stovfl
  • 14,998
  • 7
  • 24
  • 51