Recently, I have been reading a free online book called “Automate the Boring Stuff.” This book is awesome! I have learned some of the basics of the os module, shutil module, and pathlib module. This book also covers automating excel spreadsheets, and databases using SQLite.
One of the first things I learned from this book was how to use the shutil.move() method. I did this by first creating a working directory and using python to make a sub directory, making a file in the working directory, then moving this file to a sub directory. The benefit of learning such a basic task is being able to apply this same concept elsewhere on my computer. For example, my Downloads folder is always messy. However, I keep a lot of relevant folders/files in the Downloads folder until I do not need them anymore. Then, I will delete them. This way, I can use the shutil module to remove any directories with the shutil.rmtree() function or I can see what folders are empty using the os.listdir() function. Using the shutil.move() method will also allow me to find older or newer folders/files and move them to a location that makes more sense.
This is the code for my example project:
# move a file using shutil and Path librariesimport shutilfrom pathlib import Pathh = Path.cwd()# create a new directory called spam if it does not existsif (h / 'spam').is_dir(): print('this dir already exists!')else: (h / 'spam').mkdir(exist_ok=True) print('/spam dir created')if (h / 'my_file.txt').is_file(): print('this file already exists')else: # create the file with open(h / 'my_file.txt', 'w', encoding='utf') as file: file.write('hello world!')# move the file to spam folderif (h / 'spam/my_file.txt').is_file(): print('file is already in the folder')else: shutil.move(h / 'my_file.txt', h / 'spam/my_file.txt') print('file has been moved!')
Another very valuable lesson I attained from this book was the ability to use Path.glob(regex) to search for specific file suffixes to better categorize my Downloads folder.
Here is some code that I used:
from pathlib import Pathpath = Path('C:\\Users\\user\\Downloads')pdf_path = Path(path / 'EXCEL_DOCUMENTS')if pdf_path.is_dir(): print('already exists')else: pdf_path.mkdir()for file in path.glob('*.xlsx'): print('found file', file) shutil.move(file, pdf_path)
All the above code does is simply move all my excel spreadsheets to one folder name “EXCEL_DOCUMENTS” is my Downloads folder. This makes it a little more convenient when looking for a spreadsheet I downloaded a month or two ago.
Before finishing this post and telling you to go check out this online book, here is some more basic code I was able to create based on the book.
import osfrom pathlib import Pathimport sqlite3# data types for sqlite# NULL# TEXT# INT# REAL# BLOB# create the db if the file does not existif not os.path.exists('example.db'): conn = sqlite3.connect('example.db', isolation_level=None) query = 'create table if not exists people (name text not null, age int) strict' conn.execute(query) conn.close()else: conn = sqlite3.connect('example.db', isolation_level=None) query = 'insert into people values ("me", 58)' conn.execute(query) # fetch the insert statement select = 'select * from people' results = conn.execute(select).fetchall() print(results)
Very basic knowledge for someone looking to automate more of their computer tasks without having to google for programs that you may have to download/install that will inevitably do much more than the one thing you need it to do. A little advice on learning new skills for whatever endeavor you may be taking on, take little bite sized lessons. Do not do too much all at once because it will cause burn-out quickly. Also, you want to learn at a pace which will allow you to retain the information being consumed. Whatever pace that is simply depends on the person consuming the information.
Go check out this book here: Automate The Boring Stuff
If you have gotten this far, thanks for reading!