*-------[ 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.