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

Python 根据文件夹名称将文件夹移动到目录,python,Python,我是Python新手,目前正在开发一个应用程序,根据文件夹名称将文件夹移动到特定目录 我没有收到错误或警告,但应用程序不会移动文件夹。 代码如下: import os import shutil def shorting_algorithm(): file_list = [] directory = input("Give the directory you want to search.") newdir = "D:\\Torrents\\Complete\\Ude

我是Python新手,目前正在开发一个应用程序,根据文件夹名称将文件夹移动到特定目录

我没有收到错误或警告,但应用程序不会移动文件夹。 代码如下:

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

注意:我尝试删除“'”+…+“'”,但也没有成功。有什么想法吗?

在连接文件和目录时,不要忘记文件分隔符

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)

在连接文件和目录时,不要忘记文件分隔符

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)

您不需要for循环或if语句。您已经在主代码块中标识了该文件。由于要显式指定目录和文件名,因此不需要通过目录列表进行循环来查找。当您希望程序自动查找符合某些特定条件的文件时,这一点更为重要。试试这个:

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

去掉多余的引号并将斜杠添加到路径中,将newdir转换为原始字符串以避免转义,以及去掉for循环应该可以使此代码正常工作。我刚刚测试了它,它在这里工作。

您不需要for循环或if语句。您已经在主代码块中标识了该文件。由于要显式指定目录和文件名,因此不需要通过目录列表进行循环来查找。当您希望程序自动查找符合某些特定条件的文件时,这一点更为重要。试试这个:

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

去掉多余的引号并将斜杠添加到路径中,将newdir转换为原始字符串以避免转义,以及去掉for循环应该可以使此代码正常工作。我刚刚测试了它,它在这里工作。

问题在于你的循环,你混合了两种迭代方式。 发生的情况如下:

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)
    if name in xlist:
        shutil.move(directory + name,newdir)
应采取以下措施:

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)
    if name in xlist:
        shutil.move(directory + name,newdir)
或者也

for file in xlist: # only reason for this is if you want input check 
    if str(file) == name:
       # do whatever you need
另外,必须从输入中删除
“'”+…+“'”
,因为您将这些内容输入字符串,这将使比较变得非常混乱。
我还建议使用原始输入而不是输入

问题在于你的循环,你混合了两种迭代方式。 发生的情况如下:

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)
    if name in xlist:
        shutil.move(directory + name,newdir)
应采取以下措施:

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)
    if name in xlist:
        shutil.move(directory + name,newdir)
或者也

for file in xlist: # only reason for this is if you want input check 
    if str(file) == name:
       # do whatever you need
另外,必须从输入中删除
“'”+…+“'”
,因为您将这些内容输入字符串,这将使比较变得非常混乱。
我还建议使用原始输入而不是输入

谢谢大家的回答,按照建议使用“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()

谢谢大家的回答,按照建议使用“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()


请注意,如果在该字段中输入多个文件,
name
将成为一个大字符串。尝试先将单个文件与
os.path.join
组合移动,如下面的答案所示,请注意,如果在该字段中输入多个文件,则
name
将成为一个大字符串。尝试先将一个文件与
os.path.join结合起来移动,如下面的答案所示,两个文件都尝试过,但仍然相同,应用程序运行正常,没有错误或警告,但文件夹不会移动。请将名称作为正常输入进行尝试,无需
“+…+”
部分。确保一次只提供一个名称。还是不行吗?代码应该以这种方式工作,可能还有其他问题,我们无法从您提供的信息中检测到@ihat2我所做的更改:对于xlist中的文件:if name in files:shutil.move(directory+'\\'+files,newdir),问题不在于他在文件中循环,然后查看要移动的文件名是否“在”文件名中。oof。好地方@IosiG Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go Go。确保一次只提供一个名称。还是不行吗?代码应该以这种方式工作,可能还有其他问题,我们无法从您提供的信息中检测到@ihat2我所做的更改:对于xlist中的文件:if name in files:shutil.move(directory+'\\'+files,newdir),问题不在于他在文件中循环,然后查看要移动的文件名是否“在”文件名中。oof。好地方@IosiG Go-ahead写下,在执行移动之前,循环用于检查要移动的文件是否确实在目录中。我同意如果没有它也可以工作,但是在使用之前检查用户输入是一个很好的实践。@losiG这是有道理的。在这种情况下,它并没有真正做到这一点。循环中没有错误消息或异常捕获,因此我认为如果不包含
else:print(“没有这样的目录”)
或其他内容,那么循环就没有理由存在。也许我应该说……这是一个重要的警告lol.:/name“xlist”没有定义为“哦,是的”。我的错。我删除了它,因为如果不在列表中循环,就不再需要xlist,但我忘记删除
print
语句。现在编辑。实际上,您也可以继续删除文件列表。当然,所有这些都假定您不打算在其中放入任何可能需要循环的异常处理。循环用于在执行移动之前检查要移动的文件是否确实位于目录中。我同意如果没有它也可以工作,但是在使用之前检查用户输入是一个很好的实践。@losiG这是有道理的。在这种情况下,它并没有真正做到这一点。循环中没有错误消息或异常捕获,所以我想如果你不去