Python Forum

Full Version: Temp folder creation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am completely new to python and I want to replicate something that I am using in Excel. I want to create a .xlsx file call it test1 and do some work within the spreadsheet. Once this is done I would then like to save it in a temp folder (there may be two users using the file in the future) and save it using two variables - JobId and the other is environment. Once saved I will then push this to another destination.

How best to proceed with this?

I have seen this code and it creates a folder in the same place that my script is saved but I would prefer it to be a temp folder which would be deleted when I am finished.

mport os


def createFolder(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print ('Error: Creating directory. ' + directory)


# Example
createFolder('./test/')
# Creates a folder in the current directory called data
Fortunately, Python has a whole module dedicated to this - https://docs.python.org/3.7/library/temp...yDirectory
(Oct-02-2018, 04:21 PM)micseydel Wrote: [ -> ]Fortunately, Python has a whole module dedicated to this - https://docs.python.org/3.7/library/temp...yDirectory

I found that earlier when I was researching - I came across this code which seems to do what I need however I am having some trouble in getting it to work on my end. I am getting this bug message - AttributeError: 'module' object has no attribute 'TemporaryDirectory'

import tempfile

# create a temporary file and write some data to it
fp = tempfile.TemporaryFile()
fp.write(b'Hello world!')
# read data from file
fp.seek(0)
fp.read()
b'Hello world!'
# close the file, it will be removed
fp.close()

# create a temporary file using a context manager
with tempfile.TemporaryFile() as fp:
    fp.write(b'Hello world!')
    fp.seek(0)
    fp.read()
b'Hello world!'

# file is now closed and removed

# create a temporary directory using the context manager
with tempfile.TemporaryDirectory() as tmpdirname:
    print('created temporary directory', tmpdirname)

# directory and contents have been removed
Please, always post the full traceback in error tags
(Oct-02-2018, 06:00 PM)buran Wrote: [ -> ]Please, always post the full traceback in error tags

Error:
C:\Users\stephen.xxxxx\PycharmProjects\FirstProject\venv\Scripts\python.exe C:/Users/stephen.xxxxx/PycharmProjects/FirstProject/thirdscript.py Traceback (most recent call last): File "C:/Users/stephen.xxxxxx/PycharmProjects/FirstProject/thirdscript.py", line 23, in <module> with tempfile.TemporaryDirectory() as tmpdirname: AttributeError: 'module' object has no attribute 'TemporaryDirectory' Process finished with exit code 1
Apologies - I am new to the forum so I need to read up on the charter on such things.
What version of python you use? it was introduced in python 3.2...
(Oct-03-2018, 09:41 AM)buran Wrote: [ -> ]What version of python you use? it was introduced in python 3.2...

I am using version 2.7.11. So the 'tempfile' package will not work with my version?
Is there something that I could look to use in my version that will replicate somewhat this function?
tempfile module is available in python 2.7. However tempfile.TemporaryFolder() was introduced in 3.2 That is why you get error there, not earlier when you use tempfile.TemporaryFile().
Few points - you use tempfile.TemporaryFile() to create the file. Note that it will be destroyed immediately when you close it. I think you want to use tempfile.mkstemp() in which case YOU are responsible to delete it after. By default temporary file it is created in the default temp folder for the current OS. In 2.7 you have also tempfile.mkdtemp() which creates temporary directory and again you are responsible to delete it. In short, I think you need to use mkstemp() and possibly mkdtemp(), not the objects directly.
Finally, you should move to python3 as python2 support ends 1 Jan 2020.