Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. import os
  2. import tempfile
  3.  
  4.  
  5. file_descriptor, file_path = tempfile.mkstemp(suffix='.tmp')
  6.  
  7. # You can convert the low level file_descriptor to a normal open file using fdopen
  8. with os.fdopen(file_descriptor, 'w') as open_file:
  9. open_file.write('hello')
  10.  
  11. # OR without the 'with'
  12. open_file = os.fdopen(file_descriptor, 'w')
  13. open_file.write('hello')
  14. open_file.close()
  15.  
  16. # OR without the open low level file_descriptor
  17. os.close(file_descriptor)
  18. open_file = open(file_path, 'w')
  19. open_file.write('hello')
  20. open_file.close()
  21.  
  22.  
  23. # You are responsibile for deleting your temp file
  24. os.unlink(file_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement