Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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
Download

Sample Directories (.zip)

51.6 KB
Download

Course Slides (.pdf)

333.6 KB
Avatar image for Robb Glietz

Robb Glietz on Aug. 4, 2020

Great video learned a ton about how files work in Python. Thank you.

Avatar image for Liam Pulsifer

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 :)

Avatar image for Robert T Paulsen

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.

Avatar image for Hilman

Hilman on Aug. 9, 2020

Beautiful, easy-to-understand series. Thank you.

Avatar image for agerbes

agerbes on Aug. 9, 2020

Awesome Tutorial. Good Job!

Avatar image for gabrielmachac

gabrielmachac on Aug. 14, 2020

Thank you for this great tutorial.

Avatar image for Ranjit Shrivastva

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.

Avatar image for johnnypham

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.

Avatar image for Liam Pulsifer

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.

Avatar image for tobenary

tobenary on Aug. 30, 2020

wow!

Avatar image for jamesbrown68

jamesbrown68 on Aug. 30, 2020

Good course, Liam. Thanks for the time and effort.

Avatar image for Liam Pulsifer

Liam Pulsifer RP Team on Sept. 8, 2020

Thanks for the comment @jamesbrown68! Glad you enjoyed the course.

Avatar image for Doug Ouverson

Doug Ouverson on April 21, 2021

Thanks Liam for a job well done! I benefited greatly from the information.

Avatar image for aniketbarphe

aniketbarphe on Sept. 26, 2021

Great One! Thank You!

Avatar image for Robert T

Robert T on Sept. 30, 2021

Great course, Liam, I learned a lot and this will help me in the future!

Avatar image for jillwellman

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.