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...
You just put your code below the defined function & make sure there's a tab in front.
To call a function it's just:
You can print out a statement using the
You can ask for input from the user by using the
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.
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).
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.
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.