简体   繁体   English

根据文件夹名称将其移动到目录

[英]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. 我是Python的新手,目前正在开发一个应用程序,该应用程序可根据文件夹的名称将文件夹移动到特定目录。

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? 有任何想法吗?

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)

You don't need the for loop or the if statement. 您不需要for循环或if语句。 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. 除去多余的引号,并在路径中添加斜杠,将newdir转换为原始字符串以避免转义,并摆脱for循环应可使此代码正常工作。 I just tested it out and it works here. 我刚刚测试了一下,它在这里有效。

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. 我也建议您使用raw_input代替输入。

Thank you all for your answers, the problem was easily solved by using "shutil.move(directory + '\\' + files,newdir)" as suggested. 谢谢大家的回答,按照建议使用“ shutil.move(directory +'\\'+ files,newdir)”可以轻松解决该问题。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM