首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shutil.move(os.path.join(…)不工作

Shutil.move(os.path.join(…)不工作
EN

Stack Overflow用户
提问于 2022-12-04 10:39:39
回答 1查看 36关注 0票数 0

这个程序创建新的目录和自动移动文件到那里,如果关键字匹配。只有我的程序的第4步不能工作-文件不会从源文件移动到目标文件夹。其他一切都能工作。*

工作如下:

我们有一个源文件夹,一个目标文件夹,一个关键字文件夹。

  1. 查找关键字文件夹中的.txt文件
  2. 创建以目标文件夹.txt文件命名的子目标目录
  3. 从.txt文件获取关键字
  4. 如果在源文件->中找到关键字,则将源代码文件从源文件夹移动到目标文件夹

G 213中的子目录

我的程序工作,除了步骤4。没有错误信息。文件就待在原处。当硬编码和使用绝对路径时,程序在我的原型中运行得很顺利。在代码中,您可以在#### NOT WORKING:下面找到它

您可以自己尝试这个程序:https://github.com/kormuch/python_file_organizer (也包括模拟用的虚拟.txt文件)

代码语言:javascript
复制
import os
import shutil


#directories with relative paths
dir_source_files = r'source files'
dir_destination = r'destination'
dir_keyword_files = r'keywords'


def create_folders_and_move_files():
    scanned_dir_keyword_files = os.scandir(dir_keyword_files)
    for file in scanned_dir_keyword_files: #creates new dirs named after .txt files in destination folder
        print((f".txt file found in dir_keyword_files:\n{file} name of new folder: " + os.path.splitext(file.name)[0]))
        new_dir_name = str(os.path.splitext(file.name)[0])
        path_for_new_dir = os.path.join(dir_destination, new_dir_name)

        try:
            os.makedirs(path_for_new_dir, exist_ok = False)
            print(f"directory creation succesful. created directory:{new_dir_name}\n")
        except OSError as error:
            print(f"directory creation not succesful. '{new_dir_name}' already exists\n")    
# now a new directory is created and has the same name as the .txt file
    scanned_dir_keyword_files = os.scandir(dir_keyword_files)
    
    for txt_file_with_keywords in scanned_dir_keyword_files:
        print(f"TASK 1: iterating through parent txt-keywordfile:\n {txt_file_with_keywords} ")
        keywordlist_a = []
        with open(txt_file_with_keywords) as txt_full_with_hashtags:
            for keyword_with_hashtags in txt_full_with_hashtags.readlines():
                keywords_without_hashtags = keyword_with_hashtags.rstrip().split('#') #automatically creates list and removes the hashtag from every keyword
                keywords_without_hashtags.remove('') #removes empty entries from list
            print(f"TASK 2: fill child keywordlist_a with keywords from parent:") 
            for keyword_without_hashtag in keywords_without_hashtags:
                keywordlist_a.append(keyword_without_hashtag)
                print('keyword added to keywordlist_a: ' + keyword_without_hashtag)
            print("keywordlist_a ready:")
            print(keywordlist_a)
                
                #### NOT WORKING:
            for scanned_dir_destination in os.scandir(dir_destination):
                print("This is the destination path " + str(os.path.realpath(scanned_dir_destination)))                    
                for keyword_a in keywordlist_a:
                        for scanned_scource_file in os.scandir(dir_source_files):
                            print(f"TASK 3: comparing keyword '{keyword_a}' to '{scanned_scource_file}'")
                            if keyword_a.lower() in str(scanned_scource_file).lower():
                                print(f"Bingo! '{keyword_a}' found in '{scanned_scource_file}'")
                                try:
                                    shutil.move(os.path.join(scanned_scource_file.path, scanned_scource_file), scanned_dir_destination)
                                except:
                                 pass
                            
            print("\n")            
        
create_folders_and_move_files()

输出:

代码语言:javascript
复制
.txt file found in dir_keyword_files:
<DirEntry 'audiobooks.txt'> name of new folder: audiobooks
directory creation succesful. created directory:audiobooks

.txt file found in dir_keyword_files:
<DirEntry 'movies.txt'> name of new folder: movies
directory creation succesful. created directory:movies

TASK 1: iterating through parent txt-keywordfile:
 <DirEntry 'audiobooks.txt'> 
TASK 2: fill child keywordlist_a with keywords from parent:
keyword added to keywordlist_a: audiobook 
keyword added to keywordlist_a: Hörbuch 
keyword added to keywordlist_a: podcast
keywordlist_a ready:
['audiobook ', 'Hörbuch ', 'podcast']
This is the destination path C:\Users\Desktop\Desktop\Python\testing\file organizer\destination\audiobooks
TASK 3: comparing keyword 'audiobook ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
Bingo! 'audiobook ' found in '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'audiobook ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
Bingo! 'audiobook ' found in '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'Hörbuch ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'Hörbuch ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'podcast' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'podcast' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
This is the destination path C:\Users\Desktop\Desktop\Python\testing\file organizer\destination\movies
TASK 3: comparing keyword 'audiobook ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
Bingo! 'audiobook ' found in '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'audiobook ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
Bingo! 'audiobook ' found in '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'Hörbuch ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'Hörbuch ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'podcast' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'podcast' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'


TASK 1: iterating through parent txt-keywordfile:
 <DirEntry 'movies.txt'> 
TASK 2: fill child keywordlist_a with keywords from parent:
keyword added to keywordlist_a: movie 
keyword added to keywordlist_a: full film 
keyword added to keywordlist_a: star wars
keywordlist_a ready:
['movie ', 'full film ', 'star wars']
This is the destination path C:\Users\Desktop\Desktop\Python\testing\file organizer\destination\audiobooks
TASK 3: comparing keyword 'movie ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'movie ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'full film ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'full film ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'star wars' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'star wars' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
This is the destination path C:\Users\Desktop\Desktop\Python\testing\file organizer\destination\movies
TASK 3: comparing keyword 'movie ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'movie ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'full film ' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'full film ' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'star wars' to '<DirEntry 'How to Read People Like a Book -James W. Williams -Full Audiobook (192kbit_AAC).m4a.txt'>'
TASK 3: comparing keyword 'star wars' to '<DirEntry 'How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'>'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-12-04 11:36:43

将适当的行更改为:

代码语言:javascript
复制
shutil.move(scanned_scource_file, scanned_dir_destination)

您的代码不能工作的原因是由于无效的传递参数引发了错误,导致shutil.move()失败:

代码语言:javascript
复制
# NotADirectoryError: [Errno 20] Not a directory: 'source files/How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt/source files/How to Talk to Anyone 92 Little Tricks for Big Success in Relationships Audiobook (128kbit_AAC).m4a.txt'

您所面临的问题的核心是错误的期望,即scanned_source_file.path将提供没有基本文件名的目录部分,但它提供了完整的路径文件名。

请注意,将行更改为:

代码语言:javascript
复制
shutil.move(scanned_scource_file.path, scanned_dir_destination)

也会像

代码语言:javascript
复制
shutil.move(scanned_scource_file, scanned_dir_destination)

其中,第一个版本提供完整的路径文件名作为字符串,而另一个作为等效对象。

顺便提一句:使用scanned_scource_file.name而不是str(scanned_scource_file)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74674733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档