iHate2
iHate2

Reputation: 5

Moving folders to a directory according to their name

I'm new in Python and am currently developing an application that moves folders to a specific directory according their folder name.

I get no errors nor warnings but the application won't move the folders. Here's the code:

import os
import shutil

def shorting_algorithm():

    file_list = []
    directory = input("Give the directory you want to search.")
    newdir = "D:\\Torrents\\Complete\\Udemy"
    name = "'" + input("Give the name of the files you want to move.") + "'"
    xlist = os.listdir(directory)
    print(xlist)
    print(name)


    for files in xlist:
        if name in files:
            shutil.move(directory + files,newdir)


shorting_algorithm()

Note: I tried removing "'" +...+"'" but it didn't work either. Any ideas?

Upvotes: 0

Views: 103

Answers (4)

iHate2
iHate2

Reputation: 5

Thank you all for your answers, the problem was easily solved by using "shutil.move(directory + '\' + files,newdir)" as suggested.

import os
import shutil

def shorting_algorithm():
    directory = input("Give the directory you want to search.")
    name = input("Give the name of you files you want to move.")
    newdir = input("Give the new directory.")
    xlist = os.listdir(directory)    

    for files in xlist:
        if name in files:
          shutil.move(directory + '\\' + files,newdir)                 

shorting_algorithm()

Upvotes: 0

Josef Ginerman
Josef Ginerman

Reputation: 1560

The problem is your loop, you mixed two ways of iterating. What happens is the following:

for files in xlist: #loop through the names of the files
    if name in files: # look for the name of your file inside the name of another file
        shutil.move(directory + files,newdir)

What should be done is the following:

    if name in xlist:
        shutil.move(directory + name,newdir)

or also

for file in xlist: # only reason for this is if you want input check 
    if str(file) == name:
       # do whatever you need

Also, you have to remove the "'" +...+"'" from the input, since you are entering those into the string, which will make the comparison quite messy. I'd recommend also to use raw_input instead of input.

Upvotes: 0

user10597469
user10597469

Reputation:

You don't need the for loop or the if statement. You already identified the file in the main code block. Since you are specifying a directory and a filename explicitly, you don't need to do a loop through a directory list to find one. That's more for when you want a program to automatically find a file that fits some particular condition. Try this:

    import os
    import shutil

    def shorting_algorithm():

            directory = input("Give the directory you want to search.")
            newdir = r'D:\Torrents\Complete\Udemy'
            name = "\\" + input("Give the name of you files you want to move.")
            print(name)
            print(directory)
            shutil.move(directory + name,newdir)

    shorting_algorithm()

Getting rid of the extra quotes and adding your slashes to the path, turning your newdir into a raw string to avoid escapes, and getting rid of the for loop should make this code work. I just tested it out and it works here.

Upvotes: 0

Paritosh Singh
Paritosh Singh

Reputation: 6246

Don't forget the file separator while joining the file and the directory.

for files in xlist:

    #if name in files: EDIT: As pointed out by IosiG, Change here too
    if name == files:
        shutil.move(directory + files,newdir) #make changes here

directory + '\\' + files. 
#or 
import os
os.path.join(directory,files)

Upvotes: 2

Related Questions