how to use python tempfile module?

Namaste Friends,



                             In This Tutorial, I am Going To Write About What Is Python Tempfile module?, Why This Module Is Important to learn? and How To Use Python Tempfile Module?

Python TempFile Module Tutorial



Q 1. What Is Python tempfile Module?


Ans. Python Tempfile Module Is a Python Standard Library, Means This Module Comes With Python. This Module Allow User To Create Temporary Files In Most secure Manners. With This Module, Any User Easily Can Create Temporary Files Like Object In Python. This Module Is Cross Platform Supported. So, User Don't need to worry about Cross Platform Problems.
In Simple Words, This Module Provides User special Function That Can Create Temporary File Like Object Automatically and Also Delete Automatically after work finished.


Q 2. Why This Module Is Important To learn?

Ans. Many Times In Python Programming, we needs Temporary Files To Store Data Only For Temporary Usages and at that situation, This Module Comes In Situation. This Module Handle All Functions For Creating Secure Temporary Files Because Many Times In Computer, Privacy And Security Plays Major Role In Creating Highly Feature Application.


So, let's Start Our Practical Example Tutorial




Input : [1]  


#
# ==================================================
#          PYTHON tempfile MODULE TUTORIAL
# ==================================================
# 
# author   : suraj singh bisht
# contact  : SSB
#            surajsinghbisht054@gmail.com
#            https://bitforestinfo.blogspot.com
#            
# Here, For This Tutorial
#
# I am using
#
# Operating System : Ubuntu 16.04
# Python Version   : python 2.7.12
# Editor           : ipython notebook
#

Output : [1]  

  

Input : [2]  

#
# Import Module
#
import tempfile

Output : [2]  

  

Input : [3]  

#
# Templfile Module Allow us To Create Temporary File 
# In Secure Manner. In Simple Words, This Module Provide Us
# Facility To Create A Temporary File, That can Destroy itself
# automatically after doing its works. 
# With Temfile, A User Easily Can Create A Temporary File Like Object
# In Very Secure Manner, With random name that can be easy to guess,
# And With Automatic File Deleting Features.
#
# So, let's Start Our Today's Tutorial
#
# In Our First Example, I will demonstrate Simple tempfile.TemporaryFile Function.
#
# Creating Temporary File Like Object
temp_file=tempfile.TemporaryFile()

Output : [3]  

  

Input : [4]  

#
# Temporary File Name 
print temp_file.name
#
# Temporary File Object Mode, Default Mode : w+b
print temp_file.mode
#
# Writing Data In Temporary File
for i in range(5):
    temp_file.write("Line No.{}\n".format(i))

Output : [4]  

<fdopen>
w+b

Input : [5]  

#
# Seek Written Data
temp_file.seek(0)

Output : [5]  

  

Input : [6]  

#
# Reading written Data
print temp_file.read()

Output : [6]  

Line No.0
Line No.1
Line No.2
Line No.3
Line No.4

Input : [7]  

#
# Automatically Clean All Data And Close Tempfile Object
temp_file.close()

Output : [7]  

  

Input : [8]  

#
# To Open Temporary File In Other Mode
#
temp_file = tempfile.TemporaryFile(mode="w+t")
# Here 'w+t' Means Text Mode

# Write Data
temp_file.write("Example")

# Seek Data For reading from File
temp_file.seek(0)

# Read Data
print temp_file.read()

# Automatically clean and close data
temp_file.close()

Output : [8]  

Example

Input : [9]  

# Next Function,
# 
# tempfile.NamedTemporaryFile()
#   
#             This Function Works Completely Like TemporaryFile() Function
# But The Only difference is, This Function also Provides Temporary File path
# Can be accessed in Other program also.
# In Simple Works, This Function Also Provide Us File Path Of Temporary File
# That Can be Access By Other Function also.
#
# So, let's Try This Function Also
#
temp_file = tempfile.NamedTemporaryFile()

Output : [9]  

  

Input : [10]  

#
# Temporary File Name 
print temp_file.name
#
# Temporary File Object Mode, Default Mode : w+b
print temp_file.mode
#
# Writing Data In Temporary File
for i in range(5):
    temp_file.write("Line No.{}\n".format(i))
#
# Flush Written Data
temp_file.flush()

Output : [10]  

/tmp/tmpo7ZME1
w+b

Input : [11]  

#
# Now, let's try to access and read data from
# our temp file created by tempfile module,
# using normal python open() Function.
# In This Example, I Am Only Trying To Show You
# How We can access TemporaryFile, Created By Using
# tempfile.NamedTemporaryFile() Function.
#
# here, I am reading file created by tempfile module
access_tempfile=open(temp_file.name, 'r').read()

Output : [11]  

  

Input : [12]  

#
# Now, as you can see, we accessed tempfile file using normal
# open() function.
#
# Data Found In Temporary File
print access_tempfile

Output : [12]  

Line No.0
Line No.1
Line No.2
Line No.3
Line No.4

Input : [13]  

#
# Use Close() Function For Automatic Cleaning And Closing OBject
#
temp_file.close()

Output : [13]  

  

Input : [14]  

#
# Now, let's check that temporary is still present in its previous 
# address, after using close() function
#
try:
    access_tempfile=open(temp_file.name, 'r').read()
except Exception as e:
    print e

Output : [14]  

[Errno 2] No such file or directory: '/tmp/tmpo7ZME1'

Input : [15]  

#
# Wow, As You Can See, Above Error is Showing That
# Temporary File Is Not present In its Address.
# As I already Said, This Module Handles All Function 
# Automatically To Provide Secure Temporary File.
#
#
# Some Other Useful Function Of Tempfile Module
#
# Provide Us Temporary Directory
print tempfile.gettempdir()
#
# Create Temporary Directoy In Most Secure Manners
print tempfile.mkdtemp()
#
# Creates Temporary File In Most Secure Manners
print tempfile.mkstemp()
#
#
# This Function Also Completely Works Like TemporaryFile()
# Function, Except This Function provide additional Features 
# for cleaning Data after crossing specific limit size.
print tempfile.SpooledTemporaryFile()

Output : [15]  

  

Input : [16]  

# This Tutorial Ends Here
#
# Reference :
#
# Python Official Documentation
#
#              https://docs.python.org/2/library/tempfile.html
#
#

Output : [16]  

  


Written By
            SSB

Share this

Related Posts

Previous
Next Post »