Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Temp folder creation
#1
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
Reply
#2
Fortunately, Python has a whole module dedicated to this - https://docs.python.org/3.7/library/temp...yDirectory
Reply
#3
(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
Reply
#4
Please, always post the full traceback in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
What version of python you use? it was introduced in python 3.2...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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?
Reply
#8
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,390 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  pyspark creating temp files in /tmp folder aliyesami 1 4,820 Oct-16-2021, 05:15 PM
Last Post: aliyesami
  Help with storing temp data for each day then recording min/max in app. trthskr4 3 2,356 Sep-10-2021, 10:51 PM
Last Post: trthskr4
  How to save Matplot chart to temp file? Morkus 2 4,435 Jun-12-2021, 10:52 AM
Last Post: Morkus
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,434 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 4,944 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Get system info from PI (cpu load and temp) korenron 2 2,049 Aug-04-2019, 08:45 AM
Last Post: korenron
  folder PyQt5 created during creation exe atlass218 0 1,518 Apr-26-2019, 06:25 PM
Last Post: atlass218
  temp analysis Simba 13 5,743 Apr-25-2019, 09:13 PM
Last Post: Simba

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020