Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python A simple Python script to organize files on your desktop

Ghost

Platinum Coder
Hey all,

I want to preface this by saying that I am no expert in Python. I only just started learning, but have wanted to for a while.
I agreed to help my father organize his files, and I quickly found that everything was so scattered that I needed some help from a script.
I decided to write my first Python script to take image files & put them in a new folder called /images/, and do the same with .txt, .doc, etc files in /documents/
I did not develop the script further yet because I still need to go back to organize his PC, but here's what I got so far & it works!

I installed Python 3 to get this up & running, so I simply run it with "python filename.py" in the command line - in the directory my file is located.
This script only looks for files in the current directory, so I used it to quickly grab files in his desktop. I then moved the file to other disorganized folders. I did it this way to avoid any sort of crazy loop because SOME of his folders ARE organized (pictures / docs are related) and I didn't want the files to be split up.

[CODE lang="python" title="File Organizer Python Script"]import os, glob
def prompt():
startAnswer = input("Are you ready to start? Type 'yes' to begin...")
if startAnswer.lower() == 'yes':
organize()
else:
print("Your answer must be 'Yes' to begin...")
prompt()

def organize():
print("Starting to organize...")
os.chdir("./")
types = ("*.png", "*.jpg", "*.jpeg", "*.gif")
for type in types:
for file in glob.glob(type):
os.rename("./"+file, "./images/"+file)

types = ("doc", "docx", "txt")
for type in types:
for file in glob.glob("*."+type):
os.rename("./"+file, "./documents/"+file)

end()

def end():
print("The script has sucessfully run...")
endAnswer = input("Type any key to close out...")

prompt()
[/CODE]

I created 3 functions.
1) prompt() forces the user to say 'Yes' (capitalization does not matter) to start the script
2) organize() actually moves the files in the directory the python script is located in
3) end() tells the user that the script has finished & they can click any key to close out of the script

As you can see, the syntax for Python is very simple.
to define a function it's just...
def functionNameHere():
You just put your code below the defined function & make sure there's a tab in front.

To call a function it's just:
functionNameHere()

You can print out a statement using the print("text goes here") function.
You can ask for input from the user by using the input("Question goes here") function.
You can also set the result of the input() to a variable, to analyze the variable. I use the variableName.lower() function to evaluate the lowercase version of the inputted text.

I had to import OS & glob so that I could access the operating system files and use glob to select eligible files.
I could have created a function for this, but I actually added the functions to my script just 5 minutes ago whereas the original loop was created last week.
I originally ran the script without any prompts or text feedback, so that's why there's no function in place for the loop.


os.chdir("./")
This is where I choose the directory I would like to search. I use ./ to represent the current directory that the script is located in (Desktop).

types = ("*.png", "*.jpg", "*.jpeg", "*.gif")
this is where I declare the image types I am looking for, in an array.

[CODE lang="python"]
for type in types:
for file in glob.glob(type):
os.rename("./"+file, "./images/"+file)
[/CODE]
Finally, I just have a basic loop that says for each type in the Types array, create a glob searching for that file type. If it's there, rename (allows me to delete original file / move file) it to images/filename, so it moves from desktop to the /images/ folder I created on the desktop. It should be noted that this method cannot create a new directory, so the images/ folder had to be created first.

The only difference in the next loop is that instead of including the * asterisk (to represent a wildcard string/character) in the types array, I added it to the type below. This is just a personal preference for the second loop.
I could have created one bigger loop or a function to handle different file extensions, but I have left it as is for now.
 
That's actually a really good application and would be useful for my dad who has about a million things on his desktop.
I'll probably expand it soon to actually recursively go through folders but add an approval question for each subfolder so you can skip folders you don't want auto sorted.

Glad you like it :)
Install Python and feel free to use it!
 
Hey all,

I want to preface this by saying that I am no expert in Python. I only just started learning, but have wanted to for a while.
I agreed to help my father organize his files, and I quickly found that everything was so scattered that I needed some help from a script.
I decided to write my first Python script to take image files & put them in a new folder called /images/, and do the same with .txt, .doc, etc files in /documents/
I did not develop the script further yet because I still need to go back to organize his PC, but here's what I got so far & it works!

I installed Python 3 to get this up & running, so I simply run it with "python filename.py" in the command line - in the directory my file is located.
This script only looks for files in the current directory, so I used it to quickly grab files in his desktop. I then moved the file to other disorganized folders. I did it this way to avoid any sort of crazy loop because SOME of his folders ARE organized (pictures / docs are related) and I didn't want the files to be split up.

[CODE lang="python" title="File Organizer Python Script"]import os, glob
def prompt():
startAnswer = input("Are you ready to start? Type 'yes' to begin...")
if startAnswer.lower() == 'yes':
organize()
else:
print("Your answer must be 'Yes' to begin...")
prompt()

def organize():
print("Starting to organize...")
os.chdir("./")
types = ("*.png", "*.jpg", "*.jpeg", "*.gif")
for type in types:
for file in glob.glob(type):
os.rename("./"+file, "./images/"+file)

types = ("doc", "docx", "txt")
for type in types:
for file in glob.glob("*."+type):
os.rename("./"+file, "./documents/"+file)

end()

def end():
print("The script has sucessfully run...")
endAnswer = input("Type any key to close out...")

prompt()
[/CODE]

I created 3 functions.
1) prompt() forces the user to say 'Yes' (capitalization does not matter) to start the script
2) organize() actually moves the files in the directory the python script is located in
3) end() tells the user that the script has finished & they can click any key to close out of the script

As you can see, the syntax for Python is very simple.
to define a function it's just...
def functionNameHere():
You just put your code below the defined function & make sure there's a tab in front.

To call a function it's just:
functionNameHere()

You can print out a statement using the print("text goes here") function.
You can ask for input from the user by using the input("Question goes here") function.
You can also set the result of the input() to a variable, to analyze the variable. I use the variableName.lower() function to evaluate the lowercase version of the inputted text.

I had to import OS & glob so that I could access the operating system files and use glob to select eligible files.
I could have created a function for this, but I actually added the functions to my script just 5 minutes ago whereas the original loop was created last week.
I originally ran the script without any prompts or text feedback, so that's why there's no function in place for the loop.


os.chdir("./")
This is where I choose the directory I would like to search. I use ./ to represent the current directory that the script is located in (Desktop).

types = ("*.png", "*.jpg", "*.jpeg", "*.gif")
this is where I declare the image types I am looking for, in an array.

[CODE lang="python"]
for type in types:
for file in glob.glob(type):
os.rename("./"+file, "./images/"+file)
[/CODE]
Finally, I just have a basic loop that says for each type in the Types array, create a glob searching for that file type. If it's there, rename (allows me to delete original file / move file) it to images/filename, so it moves from desktop to the /images/ folder I created on the desktop. It should be noted that this method cannot create a new directory, so the images/ folder had to be created first.

The only difference in the next loop is that instead of including the * asterisk (to represent a wildcard string/character) in the types array, I added it to the type below. This is just a personal preference for the second loop.
I could have created one bigger loop or a function to handle different file extensions, but I have left it as is for now.
You are quoting 'Yes', expect your wanted inputted answer is 'yes', a fix would be:
Python:
def prompt():
    startAnswer = input("Are you ready to start? Type 'yes' to begin...")
    if startAnswer.lower() == 'yes' or 'Yes':
        organize()
    else:
        print("Your answer must be 'Yes' to begin...")
        prompt()
 
You are quoting 'Yes', expect your wanted inputted answer is 'yes', a fix would be:
Python:
def prompt():
    startAnswer = input("Are you ready to start? Type 'yes' to begin...")
    if startAnswer.lower() == 'yes' or 'Yes':
        organize()
    else:
        print("Your answer must be 'Yes' to begin...")
        prompt()
Well, the lower() actually turns their answer into lowercase so that it's always 'yes' or not 'yes'. Even 'Yes' will evaluate as true because 'Yes' with .lower() will be 'yes'.
To add to this though, you could use .strip() to make sure there's no spaces before or after so that ' YeS ' would still be equal to 'yes' after lower() and strip() are used!
 
Well, the lower() actually turns their answer into lowercase so that it's always 'yes' or not 'yes'. Even 'Yes' will evaluate as true because 'Yes' with .lower() will be 'yes'.
To add to this though, you could use .strip() to make sure there's no spaces before or after so that ' YeS ' would still be equal to 'yes' after lower() and strip() are used!
Okay didn't see that.
 
Hey all,

I want to preface this by saying that I am no expert in Python. I only just started learning, but have wanted to for a while.
I agreed to help my father organize his files, and I quickly found that everything was so scattered that I needed some help from a script.
I decided to write my first Python script to take image files & put them in a new folder called /images/, and do the same with .txt, .doc, etc files in /documents/
I did not develop the script further yet because I still need to go back to organize his PC, but here's what I got so far & it works!

I installed Python 3 to get this up & running, so I simply run it with "python filename.py" in the command line - in the directory my file is located.
This script only looks for files in the current directory, so I used it to quickly grab files in his desktop. I then moved the file to other disorganized folders. I did it this way to avoid any sort of crazy loop because SOME of his folders ARE organized (pictures / docs are related) and I didn't want the files to be split up.

[CODE lang="python" title="File Organizer Python Script"]import os, glob
def prompt():
startAnswer = input("Are you ready to start? Type 'yes' to begin...")
if startAnswer.lower() == 'yes':
organize()
else:
print("Your answer must be 'Yes' to begin...")
prompt()

def organize():
print("Starting to organize...")
os.chdir("./")
types = ("*.png", "*.jpg", "*.jpeg", "*.gif")
for type in types:
for file in glob.glob(type):
os.rename("./"+file, "./images/"+file)

types = ("doc", "docx", "txt")
for type in types:
for file in glob.glob("*."+type):
os.rename("./"+file, "./documents/"+file)

end()

def end():
print("The script has sucessfully run...")
endAnswer = input("Type any key to close out...")

prompt()
[/CODE]

I created 3 functions.
1) prompt() forces the user to say 'Yes' (capitalization does not matter) to start the script
2) organize() actually moves the files in the directory the python script is located in
3) end() tells the user that the script has finished & they can click any key to close out of the script

As you can see, the syntax for Python is very simple.
to define a function it's just...
def functionNameHere():
You just put your code below the defined function & make sure there's a tab in front.

To call a function it's just:
functionNameHere()

You can print out a statement using the print("text goes here") function.
You can ask for input from the user by using the input("Question goes here") function.
You can also set the result of the input() to a variable, to analyze the variable. I use the variableName.lower() function to evaluate the lowercase version of the inputted text.

I had to import OS & glob so that I could access the operating system files and use glob to select eligible files.
I could have created a function for this, but I actually added the functions to my script just 5 minutes ago whereas the original loop was created last week.
I originally ran the script without any prompts or text feedback, so that's why there's no function in place for the loop.


os.chdir("./")
This is where I choose the directory I would like to search. I use ./ to represent the current directory that the script is located in (Desktop).

types = ("*.png", "*.jpg", "*.jpeg", "*.gif")
this is where I declare the image types I am looking for, in an array.

[CODE lang="python"]
for type in types:
for file in glob.glob(type):
os.rename("./"+file, "./images/"+file)
[/CODE]
Finally, I just have a basic loop that says for each type in the Types array, create a glob searching for that file type. If it's there, rename (allows me to delete original file / move file) it to images/filename, so it moves from desktop to the /images/ folder I created on the desktop. It should be noted that this method cannot create a new directory, so the images/ folder had to be created first.

The only difference in the next loop is that instead of including the * asterisk (to represent a wildcard string/character) in the types array, I added it to the type below. This is just a personal preference for the second loop.
I could have created one bigger loop or a function to handle different file extensions, but I have left it as is for now.
Is this something you are going to run manually, or do you plan to have it run automatically on either startup or login?
 
Back
Top Bottom