Practical Recipes for Working With Files in Python (Summary)
You now know how to use Python to perform the most common operations on files and groups of files. You’ve learned about the different built-in modules used to read, find, and manipulate them.
You’re now equipped to use Python to:
- Get directory contents and file properties
- Create directories and directory trees
- Find patterns in filenames
- Create temporary files and directories
- Move, rename, copy, and delete files or directories
- Read and extract data from different types of archives
- Read multiple files simultaneously using
fileinput
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Over the course of this lesson, you’ve really learned a heck of a lot about working with files in Python. You can now create, modify and delete all kinds of files, from temporary files to normal files, to directories, to ZIP and TAR archives.
00:16
You can also copy, move, or rename files and directories of various kinds. You can traverse directory trees, you can match patterns, you can get file properties, and you can open multiple files at the same time using the fileinput
module.
00:30 That’s a pretty amazing series of things to have learned, so congratulations on making it through this whole series. I hope that you enjoyed it because I certainly had a good time making it.
Liam Pulsifer RP Team on Aug. 5, 2020
Glad to hear it @Robb Glietz! I hope you get a chance to put some of this to use in your own projects :)
Robert T Paulsen on Aug. 7, 2020
Nice job - I learned some interesting things. The zip section did get a bit cluttered as you noted.
Hilman on Aug. 9, 2020
Beautiful, easy-to-understand series. Thank you.
agerbes on Aug. 9, 2020
Awesome Tutorial. Good Job!
gabrielmachac on Aug. 14, 2020
Thank you for this great tutorial.
Ranjit Shrivastva on Aug. 16, 2020
Great stuff from handling of files in python. got something new which can be used in my current project. great job. Thanks for this great tutorial.
johnnypham on Aug. 20, 2020
Question about slow execution time shutil.make_archive
.
shutil.make_archive
took several minutes to get back to my Linux prompt. Why did that happen? How can we improve the execution time?
Here is my code:
#!/usr/local/bin/python3.8
import os
import shutil
import zipfile
shutil.make_archive('made_with_shutil.zip', 'zip')
Thanks, Johnny P.
Liam Pulsifer RP Team on Aug. 23, 2020
Thanks for the kind comments, all!
@johnnypham, interesting issue you’re having! If you don’t mind my asking, what directory are you running this script from? I notice you haven’t specified a root directory from which to create the archive, so if you ran this from your top-level directory on your machine, it would be zipping all the files on your server, which would take a while. It would probably be safer as a test script to specify the root directory explicitly. You could also add a print statement to print out the current working directory using the os module so that you can be a little more sure what’s going on.
tobenary on Aug. 30, 2020
wow!
jamesbrown68 on Aug. 30, 2020
Good course, Liam. Thanks for the time and effort.
Liam Pulsifer RP Team on Sept. 8, 2020
Thanks for the comment @jamesbrown68! Glad you enjoyed the course.
Doug Ouverson on April 21, 2021
Thanks Liam for a job well done! I benefited greatly from the information.
aniketbarphe on Sept. 26, 2021
Great One! Thank You!
Robert T on Sept. 30, 2021
Great course, Liam, I learned a lot and this will help me in the future!
jillwellman on May 4, 2022
Excellent course. Complete. Efficiently presented. I love having the video to go through for an overview. Will return and work through the examples semi-independently to get the skills I need right now. Thank you for your work!
Become a Member to join the conversation.
Robb Glietz on Aug. 4, 2020
Great video learned a ton about how files work in Python. Thank you.