2

I have a code that locates files in a folder for me by name and moves them to another set path.

import os, shutil
files = os.listdir("D:/Python/Test")

one = "one"
two = "two"
oney = "fold1"
twoy="fold2"

def findfile(x,y):
    for file in files:
        if x in file.lower():
            while x in file.lower():
              src = ('D:/Python/Test/'+''.join(file))
              dest = ('D:/Python/Test/'+y)
              if not os.path.exists(dest):
                os.makedirs(dest)
              shutil.move(src,dest)
              break

findfile(one,oney)
findfile(two,twoy)

In this case, this program moves all the files in the Test folder to another path depending on the name, let's say one as an example:

If there is a .png named one, it will move it to the fold1 folder. The problem is that my code does not distinguish between types of files and what I would like is that it excludes the folders from the search.

If there is a folder called one, don't move it to the fold1 folder, only move it if it is a folder! The other files if you have to move them.

The files in the folder contain the string one, they are not called exactly that.

I don't know if I have explained myself very well, if something is not clear leave me a comment and I will try to explain it better!

Thanks in advance for your help!

andyio
  • 129
  • 1
  • 6
  • 1
    `files` is a boolean ... what do you think will `for file in files:` do? – Patrick Artner Jan 13 '21 at 08:16
  • 3
    Research `os.walk` and `glob.iglob` for copying and use conditions in the files found to decide what to do with them. [do-i-understand-os-walk-right](https://stackoverflow.com/questions/10989005/do-i-understand-os-walk-right) and [how-to-use-glob-to-find-files-recursively](https://stackoverflow.com/questions/2186525/how-to-use-glob-to-find-files-recursively) – Patrick Artner Jan 13 '21 at 08:17
  • Sorry, I've already updated the code – andyio Jan 13 '21 at 08:17
  • what do you think does the while loop accomplish? It is a loop that always breaks out of itself after 1 run through it ==> it is obsolete. – Patrick Artner Jan 13 '21 at 08:22
  • while there is a file "x" in the folder, it will be moved to its destination path – andyio Jan 13 '21 at 08:24
  • The file you are operating on is set by `for file in files:` ... it does not change - so it either has x in it or not - no need to loop - check and perform action. The next file profided by `for file in files:` will also have x in it or not , perform action or not. You never need to move the same file over multiple times ... please debug your code and put print statements in it to see for yourself- – Patrick Artner Jan 13 '21 at 08:27
  • if i remove the `while` from the code and there are 5 files that contains "one", it will only move one of these five files – andyio Jan 13 '21 at 08:31
  • You operate on a prefilled list of files, every one of them will be handled. If there are 5 files in your directory that include something your "list of files" will be at least 5 long ... and every single one of them will be tested for having X in it and eventually moved. – Patrick Artner Jan 13 '21 at 08:33
  • yes, I tried it with the above code and it doesn't move all the files if I remove the while – andyio Jan 13 '21 at 08:36

1 Answers1

0
os.path.isdir(path)

os.path.isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows symbolic link, that means if the specified path is a symbolic link pointing to a directory then the method will return True.

Check with that function before.

Home this helps :)