DEV Community

Cover image for Dead Simple Python: Working with Files

Dead Simple Python: Working with Files

Jason C. McDonald on December 21, 2019

Like the articles? Buy the book! Dead Simple Python by Jason C. McDonald is available from No Starch Press. For me, any project doesn't start f...
Collapse
 
sandordargo profile image
Sandor Dargo

Nice and detailed article. I saw a typo that might be worth to fix. When you write about readline(), the section title is readlines(). So there are two sections called readlines().
Thanks for the post!

Collapse
 
codemouse92 profile image
Jason C. McDonald

Good catch! Fixing that now. Thanks.

Collapse
 
zacharythomasstone profile image
Zachary Stone

Would love to see the rest of the planned series! I'm sure 2020 has thrown your scheduling off. But I am really enjoying your teaching style and humor!

Collapse
 
codemouse92 profile image
Jason C. McDonald

Heheh, there's more coming, I promise! 2020 has indeed thrown me off, and I've been catching up on the book itself (which is 3/5 done). The latest plan is on the first post in the series.

Collapse
 
zacharythomasstone profile image
Zachary Stone

Excited to hear that! I tried my hand at teaching Python, but took the approach of teaching someone who doesn't know programming at all. So it's much more simple.

Hope all is well! Thank you for continuing to chip away at this series.

Collapse
 
korakot profile image
Korakot Chaovavanich • Edited

Please mention operator / in pathlib too.
Also, read_text() which is very convenient.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Thanks for the suggestions. The book will go into more depth than I can explore in the article, and I'll be sure to include these in that!

Collapse
 
kientphan profile image
KienTPhan

In the bulletpoint "You must remember to close any file you open. You can do so manually with open(), or...", I think you mean close() right?

Collapse
 
hasii2011 profile image
Humberto A Sanchez II

Nice and to the point

Collapse
 
rpural profile image
Robert Nix

In your examples of writelines(), you pass a list containing a single string element, but in practice, isn’t the usefulness of writelines() in passing several individual lines in a list?

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Hmm, I appear to be doing exactly that? I'm not sure what you mean.

lines = [
    "Finally back safe and sound\n",
    "from one of the weirdest days\n",
    "at Gravity Falls.\n"
]
with open("journal3.txt", 'w') as file:
    file.writelines(lines)