-
Notifications
You must be signed in to change notification settings - Fork 109
file iteration #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Nice work @kylerlaird that you are moving away from Bash. "It is good practice to use the |
The use of with is superfluous in this case. The use of "my_file" is pointless. These should be equivalent.
|
I agree that with is a good practice and a good tool to have (when needed for an object which is used multiple times). The pattern of "loop through all of the lines in a file" is so common, though, that it's a shame to add extra crud to what should be simple in Python. |
It depends what you're doing. If you're simply looping over a single file, that's fine. However, if you're looping over multiple files, depending what happens with the garbage collector, you could run out of file descriptors from the OS. This would rarely happen in practice in CPython because it uses reference counting, but it's difficult to guarantee that it wouldn't happen in Pypy, which uses tracing GC. Additionally, while the current implementation of You're right that |
I'm enjoying your document! I've been moving away from Bash to Python scripting for years. I'm always looking for better ways to do it.
I'm curious about the section on iterating through lines of a file. You provide some relatively complicated ways to do it using "with", readline(), etc. Why not simply "for line in open(file):"?
The text was updated successfully, but these errors were encountered: