Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. β Irrespective of whatever work you do, you definitely have a lot going on with the files, folders on your computers. There are a LOT OF THEM. Today, let look at Python's shutil module which can organize them in few lines of code. β π₯ Import/Load the modules and get ready os module will be used to interact (access files) with operating system. Quick refresher here.
In:
import shutil, os
Out:
β π Copy files
In:
shutil.copy("sample.txt", "copy_sample.txt")
Out:
'copy_sample.txt'
β β Check if copied (or any) file exists
In:
print(os.path.exists("copy_sample.txt"))
Out:
True
β πΎ Copy entire folders
In:
shutil.copytree("myfolder", "myfolder_backup") print(os.path.exists("myfolder_backup"))
Out:
True
β ποΈ Delete (carefully) the entire folder
In:
shutil.rmtree("myfolder_backup") print(os.path.exists("myfolder_backup"))
Out:
False
β π Instant backup (zip) a folder
In:
shutil.make_archive("project_backup", "zip", "myfolder") print(os.path.exists("project_backup.zip"))
Out:
True
β π Instant restore (unzip) a folder
In:
shutil.unpack_archive("project_backup.zip", "restored_folder") print(os.path.exists("restored_folder"))
Out:
True
β π Check free space on your computer
In:
total, used, free = shutil.disk_usage(".") print((free // (1024**3)), "GB free")
Out:
118 GB free
β π§ Find if a tool exists on your computer and its location
In:
print(shutil.which("python"))
Out:
/usr/bin/python/python310/python.exe
βπ£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below πΌ
β |
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. I am sure when you print C:\new_folder your code breaks due to \. Or you wonder how to print single (It's) or double ("Hello") quotes. Enter Escape Sequences, tiny backslash commands in Python that fix all of this. π Most common uses: Format using new lines Print tabs Single or double quotes in text Write file paths safely Print symbols like Ο orβ€οΈ Let's Begin... π \n Print in next line - the one we all...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Think about the time you waste right-clicking and saving course lectures on YouTube one-by-one. Inefficient process, can cause errors and you lose focus. pytubefix eliminates that bottleneck. Today is day 3 of mastering YouTube with Python. You already know how to get stats of a single video; or download them in various formats. Now time to level up with playlists. Pytubefix has a Playlist object that...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Yesterday we introduced pytubefix - a powerful module to access YouTube. We downloaded a video as well. Cool, but basic. What if you want 1080p instead of 480p. Or prefer WebM over MP4. To answer all this - let's cover downloads in depth today. π€« I am 100% sure you can charm recruiters or your colleagues as they won't know this! π₯ Code Recap: How to download YouTube video In: # Import library from...