Home > Software design >  shutil.move(os.path.join(...) does not work
shutil.move(os.path.join(...) does not work

Time:12-04

This program creates new directories and automatically moves files there, if keywords match. Only step 4 of my program does not work - files are not moved from source foulder to destination folder. Everything else works.*

Works as follows:

we have a [source folder], we have a [destination folder], we have a [keyword folder]

  1. Look for .txt file in [keyword folder]
  2. Create child destination directory named after .txt file in [destination folder]
  3. Get keywords from .txt file
  4. If keyword found in sourcefile -> move sourcefile from [source folder] to child in [destination folder]

My program works, except for step 4. No Error message. Files just stay where they are. Program ran smoothly in my prototype when hardcoded and used absolute paths. In the code you find it under #### NOT WORKING:

You can try the program yourself: https://github.com/kormuch/python_file_organizer (also in includes dummy .txt files for simulation purposes)

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

Output:

.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'>'

CodePudding user response:

Change the appropriate line to:

shutil.move(scanned_scource_file, scanned_dir_destination)

The reason why your code doesn't work was failing shutil.move() because of not valid passed parameter raising the error:

# 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'
  • Related