Jump to content

Python File Sorter Help

SgtBot

Hi,

I am working on a file sorter in python to sort different file types into their respective folders so I can go through them later. Here is my code so far:

import os

loop = 'y'

while loop == 'y':
    count = 0
    
    fileType = input("What type of file would you like to sort? ")
    
    fileList = []

    for files in os.walk("."):
        for filename in files[2]:
            fileList.append(filename)

    for i in range(len(fileList)):
        if fileList[i].split('.')[1] == fileType:
            if i == 0:
                try:
                    os.mkdir(fileType)
                except FileExistsError:
                    print("File already exists, continuing...")
                    pass
            os.system("move "+fileList[i]+" "+fileType)
            count = count+1
        else:
            break

    if count != 0:
        print("Moved", count, "file(s) into folder:", fileType)
    else:
        print("No files matching that type.")
        
    loop = input("\nSort more files? (y/n): ")

It works great, but the only problem that I have is that it seems to bug out whenever the file name itself has a space in it. For example, when I tried to make it move a .docx document with the title: AS121 - HW10 - REGULATORY GUIDANCE, CERTIFICATES AND DOCUMENTS-2.docx, it said it moved the file, but did nothing instead. Here is a screenshot of cmd while it's running:

image.png.ca90d638e32aab0037de77687b1763b0.png

I'm not sure why it would be doing this... I checked the fileList and split lines in my code, and it seems to split that word document perfectly fine into the name and the docx. Here is a screenshot of that:

image.png.ca85dfec73ad00f6f69d0802654c2609.png

 

I'm not sure why it could be doing this. Do I have to have some kind of error handling to tell the program what to do when there is a space in the file name? I feel like it shouldn't matter since I don't split the file by spaces I split it by a period. Any help would be greatly appreciated!

Link to comment
Share on other sites

Link to post
Share on other sites

Use os.rename or shutil.move to actually move the file.  Those commands will handle white space escaping for you.

 

You might also find filename, file_extension = os.path.splitext a useful command.  Because splitting on period is going to fail on "my.file.has.periods.docx"

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, Windows cmd and also Unix terminals do not handle files names with spaces too well. You should enclose them in single quotes.

 

Like this: 'this is a file name with spaces.txt'

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Egad said:

Use os.rename or shutil.move to actually move the file.  Those commands will handle white space escaping for you.

I switched to shutil.move for the command, so the new code looks like:

for i in range(len(fileList)):
        if fileList[i].split('.')[1] == fileType:
            if i == 0:
                try:
                    os.mkdir(fileType)
                except FileExistsError:
                    print("File already exists, continuing...")
                    pass
            shutil.move(fileList[i], fileType)
            count = count+1
        else:
            break

But when I try to run the program in my folder, it doesn't even recognize that there are any word documents. I tried to run it in an empty folder with that one word document and it worked... but it seems when in needs to sort through multiple files it has problems.

 

image.png.a98bb97311ea31b07fbbd138c9bfbb8e.png

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, Egad said:

Use os.rename or shutil.move to actually move the file.  Those commands will handle white space escaping for you.

 

You might also find filename, file_extension = os.path.splitext a useful command.  Because splitting on period is going to fail on "my.file.has.periods.docx"

not if you always take from the end of the array with 

ex = filename.split('.')[-1]

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×