CPython does close the file when it gets garbage collected, so it depends
how long you hold on to the references, if at all. But closing
it explicitly (or by the with context) does give you precise control of
when you are freeing that resource. And it is apparently not guaranteed to
close during garbage collection in any other implementation of python.

It is still good practice to close the file when you are done with the
resource, but just to show the garbage collector doing it in CPython:

$ lsof -p 30956 | wc -l
10
>>> x = open("/dev/null")
$ lsof -p 30956 | wc -l
11
>>> del x
$ lsof -p 30956 | wc -l
10
>>> def file_test():
...     x = open("/dev/null")
...
>>> file_test()
$ lsof -p 30956 | wc -l
10




On Mon, Feb 17, 2014 at 1:28 AM, Ævar Guðmundsson <
[email protected]> wrote:

>
>
> *-------[ First off { entry level, easy access }*
>
>   Opening and reading files with a one-liner in python is a cool trick to
> know, i.e.
>
> *x = open( os.path.join( "this" , "that" ) ).readlines()*
>
>   But can lead to a scenario where one forgets to close the file handle
> again, and for each time the code runs there will be a lingering file still
> open in memory causing various things to get offset.
>
>   This would be things like:
>
>
>    - ·         Marginal memory prints not being available again until at
>    reboot
>    - ·         Lingering .nfs* files on network servers.
>    - ·         General trash borderlining on a memory leak definition.
>
>    *x.close()*
>
>   With the line above ensures the file is closed again after use.
>
> *-------[ Secondly { intermidiate, efficient }*
>
>   The mess assosciated with this is one of the primary reason why the "
> *with*" statement was added to Python, the general file object was
> upgraded to inlude __enter__ and __exit__ methods so when you use the with
> statement to iterate over a file as you open it, the __exit__ method will
> remember to close the file handles afterwards and x.close() is no longer
> needed.
>
>
>
> *with open( "this.file" ) as x:*
>
> *  data = x.read)*
>
>
>
>   The line after *data* can be made to run further code per line and
> *data* will be left as a list for you to manipulate once the iteration is
> done and the file is closed.
>
> *-------[ Thirdly { may want to avoid if new in python but gives more
> control over operation }*
>
>
>
>   By utilizing streams and buffer pipes you get away with not defining a
> file within your code but can make it read from stdin directly.  i.e. on
> the command line or within a shortcut launcher:
>
> *myTool < myfile.txt*
>
>
>
>   You then need this inside your script, example shows a script that grabs
> any line containing the word "*asset_token*" so when batched and ran in
> paralell will extract all cases of that line from an entire server, sort of
> like grep without the crazy flags to remember:
>
> *import sys*
>
> *parameter = "asset_token"*
>
> *data = [ n for n in sys.stdin if parameter in n ]*
>
>
>
>  This method handles the file handle closing as your script exists as the
> stream from stdin will be discontinued and your data container will be
> there as *data *for further manipulation.
>
>
>
>
>
> *---[ References*
>
> http://effbot.org/zone/python-with-statement.htm
>
> http://docs.python.org/2/tutorial/inputoutput.html
>
> http://axialcorps.com/2013/09/27/dont-slurp-how-to-read-files-in-python/
>
> http://codepad.org/Mq8YXBNf
>
>
>
>   Hope this stuff helps and you have fun with file handles!
>
>
> p.s. by using os.path as in the example above rather than manually state a
> path in string format you completely negate forward slash backward slash
> issues, so to answer your pondering "*I don´t understand why I get \\\\
> and \\*" the answer is as simple as "*Because you are not using os.path*"
> :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/57f54a5c-ecc4-4015-803f-2cdb3c37eee8%40googlegroups.com
> .
>
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0Ra7U8ucYK-AVOsZf_yUzS08aJh90_RjYMGx%2BL4v31Rw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to