Python - writing and reading from a temporary file

后端 未结 3 2120
离开以前
离开以前 2021-02-06 22:51

I am trying to create a temporary file that I write in some lines from another file and then make some objects from the data. I am not sure how to find and open the temp file s

相关标签:
3条回答
  • 2021-02-06 23:07

    As per the docs, the file is deleted when the TemporaryFile is closed and that happens when you exit the with clause. So... don't exit the with clause. Rewind the file and do your work in the with.

    with tempfile.TemporaryFile() as tmp:
        lines = open(file1).readlines()
        tmp.writelines(lines[2:-1])
        tmp.seek(0)
    
        for line in tmp:
            groupId = textwrap.dedent(line.split(':')[0])
            artifactId = line.split(':')[1]
            version = line.split(':')[3]
            scope = str.strip(line.split(':')[4])
            dependencyObject = depenObj(groupId, artifactId, version, scope)
            dependencyList.append(dependencyObject)
    
    0 讨论(0)
  • 2021-02-06 23:08

    You've got a scope problem; the file tmp only exists within the scope of the with statement which creates it. Additionally, you'll need to use a NamedTemporaryFile if you want to access the file later outside of the initial with (this gives the OS the ability to access the file). Also, I'm not sure why you're trying to append to a temporary file... since it won't have existed before you instantiate it.

    Try this:

    import tempfile
    
    tmp = tempfile.NamedTemporaryFile()
    
    # Open the file for writing.
    with open(tmp.name, 'w') as f:
        f.write(stuff) # where `stuff` is, y'know... stuff to write (a string)
    
    ...
    
    # Open the file for reading.
    with open(tmp.name) as f:
        for line in f:
            ... # more things here
    
    0 讨论(0)
  • 2021-02-06 23:30

    In case the file needs to be opened a second time, e.g. read by a different process this might cause trouble on Windows OS:

    Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

    Hence a safe solution is to create a temporary directory instead and then manually create a file therein:

    import os.path
    import tempfile
    
    with tempfile.TemporaryDirectory() as td:
        f_name = os.path.join(td, 'test')
        with open(f_name, 'w') as fh:
            fh.write('<content>')
        # Now the file is written and closed and can be used for reading.
    
    0 讨论(0)
提交回复
热议问题