Write string to text file¶
Let us understand how to write string to the text file. Here are the steps involved to write string to text file.
- Make sure the target directory is available and the user have write permissions.
- You need to have a string object to write into the file using default mode. The default mode is text or
t
. - The file object should be created in write mode using
open
function. - You can use
write
on file object to write the content into the file. - We need to make sure that file object is closed to reflect the data in the file. Until we close, we will not be able to read the contents of the file.
In [1]:
# Create folder to write the data to for practice.
!mkdir -p data
In [2]:
# We will use data folder in the current directory
# As we have created the folder, we will typically have write permissions on it
!ls -ltr|grep data
drwxrwxr-x 2 itversity itversity 111 Mar 8 02:04 data -rw-rw-r-- 1 itversity itversity 4445 Mar 8 02:04 05_overview_of_retail_data.ipynb
In [3]:
# Check if the files exists
!ls -ltr data
total 20 -rw-rw-r-- 1 itversity itversity 41 Mar 8 02:04 sample_data.txt -rw-rw-r-- 1 itversity itversity 27 Mar 8 02:04 overwrite.txt -rw-rw-r-- 1 itversity itversity 27 Mar 8 02:04 new_file.txt -rw-rw-r-- 1 itversity itversity 59 Mar 8 02:04 departments.txt -rw-rw-r-- 1 itversity itversity 54 Mar 8 02:04 append.txt
In [4]:
# You can also validate whether you are able to create files or not using touch command
!touch data/validating_write_permissions
In [5]:
!ls -ltr data
total 20 -rw-rw-r-- 1 itversity itversity 41 Mar 8 02:04 sample_data.txt -rw-rw-r-- 1 itversity itversity 27 Mar 8 02:04 overwrite.txt -rw-rw-r-- 1 itversity itversity 27 Mar 8 02:04 new_file.txt -rw-rw-r-- 1 itversity itversity 59 Mar 8 02:04 departments.txt -rw-rw-r-- 1 itversity itversity 54 Mar 8 02:04 append.txt -rw-r--r-- 1 itversity itversity 0 Mar 25 06:04 validating_write_permissions
In [6]:
!rm data/validating_write_permissions
In [7]:
!ls -ltr data
total 20 -rw-rw-r-- 1 itversity itversity 41 Mar 8 02:04 sample_data.txt -rw-rw-r-- 1 itversity itversity 27 Mar 8 02:04 overwrite.txt -rw-rw-r-- 1 itversity itversity 27 Mar 8 02:04 new_file.txt -rw-rw-r-- 1 itversity itversity 59 Mar 8 02:04 departments.txt -rw-rw-r-- 1 itversity itversity 54 Mar 8 02:04 append.txt
In [8]:
!rm data/sample_data.txt
In [9]:
content = 'Sample Text which will be written to file'
In [10]:
type(content)
Out[10]:
str
In [11]:
# Now let us create the file object in write mode
# The default file format or mode is text
file = open('data/sample_data.txt', 'w')
In [12]:
file.write?
Signature: file.write(text, /) Docstring: Write string to stream. Returns the number of characters written (which is always equal to the length of the string). Type: builtin_function_or_method
In [13]:
# Returns the size of the file
# By default the contents of the file will be overwritten if the file already exists
file.write(content)
Out[13]:
41
In [14]:
file.close()
In [15]:
!cat data/sample_data.txt
Sample Text which will be written to file