βœ‹ Stop dragging or zipping files manually


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 🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

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...