Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. β We all know how to access, create or delete files, folders on our computers. The UI is pretty simple right? However, at times you might need to do it through code. Lets say as an analyst you have to save daily sales report in a new folder every day. You would like to automate that? Here is when Python's os module comes in. β What is this os module? The os module lets your code talk to the operating system β Check where your code is running π Which files, folders are present π Create, rename, or delete folders and files π And much more... β Why you should care: Save TONs of time - whether itβs organizing files, cleaning logs, or renaming hundreds of files in one go β Snippets, Snippets... β For reference currently my folder looks like this, has mix of folders and files β π Call (bring in) the os module
In:
import os
β π getcwd() to get folder name you are working in
In:
current_dir = os.getcwd() print(current_dir)
Out: /content
β π listdir() to display all files, folders in the current directory
In:
files = os.listdir(".") print(files)
Out: ['.config', 'archive', 'draft.txt', '.ipynb_checkpoints', 'notes.txt', 'sample_data']
β β Check if a particular file is present, or not present
In:
file_1 = "notes.txt" # True because notes.txt exists print(os.path.isfile(file_1)) file_2 = "notes_2.txt" # False because notes_2.txt does not exist print(os.path.isfile(file_2))
Out:
True False β π mkdir() to create a new folder use listdir to check if new folder is created
In:
os.mkdir("test_folder") print(os.listdir("."))
Out: ['.config', 'archive', 'draft.txt', '.ipynb_checkpoints', 'notes.txt', 'sample_data', 'test_folder']
β π rename() file name use listdir to check if its really renamed
In:
os.rename("draft.txt", "final.txt") print(os.listdir("."))
Out: ['.config', 'archive', 'final.txt', '.ipynb_checkpoints', 'notes.txt', 'sample_data', 'test_folder']
β β Remove a file use listdir to check if its really removed
In:
os.remove("final.txt") print(os.listdir())
Out: ['.config', 'archive', '.ipynb_checkpoints', 'notes.txt', 'sample_data', 'test_folder']
β ποΈ Only list folder names (not files)
In:
dirs = [d for d in os.listdir(".") if os.path.isdir(d)] print(dirs)
Out: ['.config', 'archive', '.ipynb_checkpoints', 'sample_data', 'test_folder']
βπ£ That's it for today! If you liked it, please share it with anyone who will find it useful and have a great weekend! πΌ β |
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers ππ So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. π₯ Ready, Get, Set, Go! Install and...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis π₯π₯ Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy). That is where Python's demoji library comes in. From a given text you can: Extract emojis Remove them if needed Replace with textual meaning Understand sentiment and what not... Let's begin!! Install demoji...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all know Pythonβs basic container data types β lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values. However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below. In: fruits = ["apple", "banana", "apple", "cherry"] counts = {} for item in fruits: counts[item] =...