help with using temporary files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerard Flanagan

    #1

    help with using temporary files

    Hello

    I'm sure its basic but I'm confused about the error I get with the
    following code. Any help on basic tempfile usage?


    ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
    Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)]
    on win32
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>>
    >>> from tempfile import NamedTemporaryF ile
    >>>
    >>> tmp = NamedTemporaryF ile()
    >>> tmp.write("Hell o")
    >>> tmp.close()
    >>>
    >>> print tmp.name[/color][/color][/color]
    c:\docume~1\ger ard\locals~1\te mp\tmpxqn4yl[color=blue][color=green][color=darkred]
    >>>
    >>> f = open(tmp.name)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    IOError: [Errno 2] No such file or directory:
    'c:\\docume~1\\ gerard\\locals~ 1\\temp\\tmpxqn 4yl'


    Thanks

    Gerard

  • Jeremy Jones

    #2
    Re: help with using temporary files

    Gerard Flanagan wrote:
    [color=blue]
    >Hello
    >
    > I'm sure its basic but I'm confused about the error I get with the
    >following code. Any help on basic tempfile usage?
    >
    >
    >ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
    >Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)]
    >on win32
    >Type "help", "copyright" , "credits" or "license" for more information.
    >
    >[color=green][color=darkred]
    >>>>from tempfile import NamedTemporaryF ile
    >>>>
    >>>>tmp = NamedTemporaryF ile()
    >>>>tmp.write(" Hello")
    >>>>tmp.close ()
    >>>>
    >>>>print tmp.name
    >>>>
    >>>>[/color][/color]
    >c:\docume~1\ge rard\locals~1\t emp\tmpxqn4yl
    >
    >[color=green][color=darkred]
    >>>>f = open(tmp.name)
    >>>>
    >>>>[/color][/color]
    >Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    >IOError: [Errno 2] No such file or directory:
    >'c:\\docume~1\ \gerard\\locals ~1\\temp\\tmpxq n4yl'
    >
    >
    >Thanks
    >
    >Gerard
    >
    >
    >[/color]

    It gets created:

    In [24]: import tempfile
    In [25]: t = tempfile.NamedT emporaryFile()
    In [26]: t.name
    Out[26]: '/tmp/tmp9bmhap'
    In [27]: ls -l /tmp/tmp*
    -rw------- 1 jmjones jmjones 0 Nov 22 11:15 /tmp/tmp9bmhap

    In [28]: t.write("123")

    In [29]: t.flush()

    In [30]: ls -l /tmp/tmp*
    -rw------- 1 jmjones jmjones 3 Nov 22 11:15 /tmp/tmp9bmhap

    In [31]: t.close()

    In [32]: ls -l /tmp/tmp*
    ls: /tmp/tmp*: No such file or directory



    From the docstring, it gets automatically deleted on close:

    def NamedTemporaryF ile(mode='w+b', bufsize=-1, suffix="",
    prefix=template , dir=None):
    """Create and return a temporary file.
    Arguments:
    'prefix', 'suffix', 'dir' -- as for mkstemp.
    'mode' -- the mode argument to os.fdopen (default "w+b").
    'bufsize' -- the buffer size argument to os.fdopen (default -1).
    The file is created as mkstemp() would do it.

    Returns an object with a file-like interface; the name of the file
    is accessible as file.name. The file will be automatically deleted
    when it is closed.
    """

    HTH,

    - jmj

    Comment

    Working...