Skip to content

Instantly share code, notes, and snippets.

@Taisho
Forked from sligodave/gist:6539531
Created November 23, 2023 07:29
Show Gist options
  • Save Taisho/8ddab653b7f8c9661df59e4b920931cd to your computer and use it in GitHub Desktop.
Save Taisho/8ddab653b7f8c9661df59e4b920931cd to your computer and use it in GitHub Desktop.

Revisions

  1. @sligodave sligodave revised this gist Sep 12, 2013. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,18 @@
    import tempfile


    # Note: open_file here is a low level file descriptor, not an ordinary open file.
    file_descriptor, file_path = tempfile.mkstemp(suffix='.tmp')

    # You can convert the low level open_file to a normal open file using fdopen
    # You can convert the low level file_descriptor to a normal open file using fdopen
    with os.fdopen(file_descriptor, 'w') as open_file:
    open_file.write('hello')

    # OR without the with
    # OR without the 'with'
    open_file = os.fdopen(file_descriptor, 'w')
    open_file.write('hello')
    open_file.close()

    # OR without the low level file descriptor
    # OR without the open low level file_descriptor
    os.close(file_descriptor)
    open_file = open(file_path, 'w')
    open_file.write('hello')
  2. @sligodave sligodave revised this gist Sep 12, 2013. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -3,21 +3,19 @@


    # Note: open_file here is a low level file descriptor, not an ordinary open file.
    open_file, file_path = tempfile.mkstemp(suffix='.tmp')
    file_descriptor, file_path = tempfile.mkstemp(suffix='.tmp')

    # You can convert the low level open_file to a normal open file using fdopen
    with os.fdopen(open_file, 'w') as open_file:
    with os.fdopen(file_descriptor, 'w') as open_file:
    open_file.write('hello')

    # OR without the with

    open_file = os.fdopen(open_file, 'w')
    open_file = os.fdopen(file_descriptor, 'w')
    open_file.write('hello')
    open_file.close()

    # OR without the low level file descriptor

    os.close(open_file)
    os.close(file_descriptor)
    open_file = open(file_path, 'w')
    open_file.write('hello')
    open_file.close()
  3. @sligodave sligodave revised this gist Sep 12, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    import os
    import tempfile

  4. @sligodave sligodave renamed this gist Sep 12, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.txt → gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@


    import os
    import tempfile

  5. @sligodave sligodave created this gist Sep 12, 2013.
    29 changes: 29 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@


    import os
    import tempfile


    # Note: open_file here is a low level file descriptor, not an ordinary open file.
    open_file, file_path = tempfile.mkstemp(suffix='.tmp')

    # You can convert the low level open_file to a normal open file using fdopen
    with os.fdopen(open_file, 'w') as open_file:
    open_file.write('hello')

    # OR without the with

    open_file = os.fdopen(open_file, 'w')
    open_file.write('hello')
    open_file.close()

    # OR without the low level file descriptor

    os.close(open_file)
    open_file = open(file_path, 'w')
    open_file.write('hello')
    open_file.close()


    # You are responsibile for deleting your temp file
    os.unlink(file_path)