Does python make a copy of opened files in memory? -


so search filenames os.walk() , write resulting list of names file. know more efficient : opening file , writing each result find them or storing in list , writing whole list. list big wonder if second solution work.

see example:

import os fil = open('/tmp/stuff', 'w') fil.write('aaa') os.system('cat /tmp/stuff') 

you may expect see aaa, instead nothing. because python has internal buffer. writing disk expensive, has to:

  • tell os write it.
  • actually transfer data disk (on hard disk may involve spinning up, waiting io time, etc.).
  • wait os report success on writing.

if want write small things, can add quite time. instead, python keep buffer , actually write time time. don't have worry memory growth, kept @ low value. docs:

"0 means unbuffered, 1 means line buffered, other positive value means use buffer of (approximately) size (in bytes). negative buffering means use system default, line buffered tty devices , buffered other files. if omitted, system default used."

when done, make sure fil.close(), or fil.flush() @ point during execution, or use keyword buffering=0 disable buffering.

another thing consider happens if, reason, program exits in middle of process. if store in memory, lost. have on disk, remain there (but unless flush, there no guarantee of how saved).


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -