Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:01:27+00:00 2026-06-07T21:01:27+00:00

Help! I was writing a script to organize my desktop by moving files with

  • 0

Help! I was writing a script to organize my desktop by moving files with certain extensions. It seemed to work fine but when I let it loose on my Desktop something strange happened.

So I copy all the files into folders created on the fly like ‘PDFs’ and ‘IMAGES’ but for some reason when I ran the script it create no folders but files and proceeded to store the files within another file.

I need to get these guys back but I have no idea what to do. I am using Windows 7 by the way. Below is my code. Any help would be appreciated.

import shutil
import os

pwd = os.getcwd()
working_path = 'C:\\Users\\grayson\\Desktop'
wp = working_path
folders = {'pdf':'PDFS','img':'IMAGES'}

def main():
    create_folders();

    for d in os.walk(wp):
        files = d[2]
        break
    print files

    for filename in files:
        order_file( filename )


def create_folders():
    for fol in folders.keys():
        fol_name = folders[fol]
        if not os.path.isdir(fol_name):
            os.mkdir(fol_name)

def order_file(fname):
    split_name = fname.split('.')
    exten = split_name[len(split_name)-1]

    if exten == 'pdf':
        shutil.move(wp + '\\' + fname, wp + '\\' + folders['pdf'])
    elif exten == 'jpg' or exten == 'png' or exten == 'gif':
        shutil.move(wp + '\\' + fname, wp + '\\' + folders['img'])
    else:
        print 'Sumin else'

main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:01:28+00:00Added an answer on June 7, 2026 at 9:01 pm

    This is a very awkward script for essentially moving files into sorted directories. But here is my guess as to what went wrong. If you did not run this with Desktop as your current working directory (meaning you were not in this directory when you ran the script), then the folders were made in some other location. Then when your files started to move, the directories didn’t exist, so it just moved them over each other as new files.

    Unfortunately, you cannot reverse this damage. Your only hope is that you have backups.

    Let me explain some parts of your script in more detail…

    pwd = os.getcwd()
    working_path = 'C:\\Users\\grayson\\Desktop'
    

    If pwd is not the same as working_path, everything will go wrong in your script. Also, you should always use forward slashes, even on windows. Its valid and its easier for path building: working_path = 'C:/Users/grayson/Desktop'

    Your folder creation section:

    # BEFORE
    def create_folders():
        for fol in folders.keys():
            fol_name = folders[fol]
            if not os.path.isdir(fol_name):
                os.mkdir(fol_name)
    

    You don’t ensure that the directories are actually created under your working_path variable. You could have written this as:

    # AFTER
    def create_folders():
        for fol_name in folders.itervalues():
            fol_path = os.path.join(working_path, fol_name)
            if not os.path.isdir(fol_path):
                os.mkdir(fol_path)
    

    That right there would have probably saved you. It would have made the directories specifically under Desktop instead of wherever you ran the script from.

    # BEFORE
    split_name = fname.split('.')
    exten = split_name[len(split_name)-1]
    

    Lot of typing to get the extension. You could have made this easier by:

    # AFTER
    exten = fname.split('.')[-1]
    

    And lastly, the part where you move the files:

    # BEFORE
    if exten == 'pdf':
        shutil.move(wp + '\\' + fname, wp + '\\' + folders['pdf'])
    elif exten == 'jpg' or exten == 'png' or exten == 'gif':
        shutil.move(wp + '\\' + fname, wp + '\\' + folders['img'])
    else:
        print 'Sumin else'
    

    You built the paths by adding slashes and only build the destination to the directory name. If that directory didnt exist, it will assume its a file. You should have built it to the specific file name:

    # AFTER
    src = os.path.join(wp, fname)
    key = None
    
    if exten == 'pdf':
        key = 'pdf'
    elif exten in ('jpg', 'png', 'gif'):
        key = 'img'
    
    if key:
        shutil.move(src, os.path.join(wp, folders['img'], fname))
    else:
        print 'Sumin else'
    

    This way it will be sure to have failed if the directory didn’t exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script to download gzipped XML sitemaps; the files download, but they
Need help writing a script downloads data from google insight using c# this is
Help needed. Im writing a function that returns result of ajax call but i
Writing a script to help me keep my playlists synchronised between to computers. I
I'm writing a script that performs the same function several times, but when I
I need help in writing a C# script which can access a secured shared
I need help writing a TSQL script to modify two columns' data type. We
It would really make my work easier if someone could help me with writing
I Need Help in writing script which help me cleaning hash of hashes before
in jsp i am writing java script. Please help me how to do null

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.