From the course: Learning Python
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Solution: Files - Python Tutorial
From the course: Learning Python
Solution: Files
- Okay, for this challenge, we needed to add up all of the byte sizes of the text files in the depths directory. We can assume that there's a directory called depths at the same level of as our code. So that's where these files are stored. And we had to ignore any files that were not text files. So in my code, I have a variable that keeps track of the total byte count so far, and you can see that I've imported the OS module 'cause I'm going to be using that. Then I have a variable that contains the name of the depths directory. Now I need to get a list of all of the contents of the directory, the steps directory right here. And what I'm going to do is use the listdir function, which is contained within the OS module, which is part of the standard library. And that will give me a list of file names. And those file names are just names. They don't have the directory path in them. So next I have a loop which iterates over each one of the file names in the list that I get back from the…