A file is used to store your data. The data in file would persist even after the program is no longer running. In this tutorial, you will learn about the basic of file handling to read and to write the following file format
A file usually have three things in common:
File name can be any alphanumerical character and it must start with a string character. File extension indicates the type of file such as .docx for Microsoft Word document, .mp3 for music, .mp4 for movie, .txt for text file, .json for JSON file, .csv for CSV file and so on. File path represent the drive and folders (or directory) where the file is stored. On Windows, path separator are written using backslash (\) while on Linux or Mac OS path are written using slash (/)
Python has standard module called os, which is very useful for handling file. To use it, you need to import it first.
import os
# get the path name of the current folder
os.getcwd()
# change folder into C:\Users\your folder\Desktop
os.chdir('C:\\Users\\your folder\\Desktop')
os.getcwd()
# to avoid adding backslash or slash manually, you can use r'path'
myPath=r'C:\Users\your folder\Documents\Python\Files'
os.chdir(myPath)
os.getcwd()
# to write a text file (long version)
data="hello world.\nTest ABC\n123"
fileName="myText1.txt"
fp = open(fileName,'w') # fp = file pointer, 'w' means to write
fp.write(data)
fp.close()
# check if file exist
os.path.isfile(fileName)
# another shorter way is to use with, we don't need to close the file pointer
with open(fileName,'w') as fp:
fp.write(data)
# to append some more text into the existing text file
data="This is my additional line"
with open(fileName,'a') as fp: # fp = file pointer, 'a' means to append
fp.write(data)
# to read back the entire text file that we have written
with open(fileName,'r') as fp: # fp = file pointer, 'r' means to read
content=fp.read()
print(content)
# if we want to read per line, use readlines() instead of read()
with open(fileName,'r') as fp: # fp = file pointer, 'r' means to read
content=fp.readlines()
print(content)
When you have data (either a lits, dictionary or pandas data frame table), writing it into comma delimited format would make a text file with file extension .csv. Python has standard module csv to handle reading and writing CSV format. To use it, you need to import it first
import csv
We can write a Python list into CSV and read it back as Python list.
lst=["abc",1234,"680a"]
fileName="myData1.csv"
# to write a csv file
with open(fileName,'w') as fp: # fp = file pointer, 'w' means to write
writer = csv.writer(fp) # to create csv writer object
writer.writerow(lst) # write a row to the csv file
# to read csv file
with open(fileName,'r') as fp: # fp = file pointer, 'r' means to read
reader = csv.reader(fp)
for row in reader:
print(row)
print(type(row))
We can write a dictionary into CSV and read it back as dictionary.
dic={'name': 'John', 'dept': 'IT', 'month': 'April', 'year':2022}
fileName="myData2.csv"
fieldnames=dic.keys()
fieldnames
with open(fileName, mode='w') as fp:
writer = csv.DictWriter(fp, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(dic)
with open(fileName, mode='r') as fp:
reader = csv.DictReader(fp)
for row in reader:
print(row)
print(type(row))
We can write a Pandas data frame table into CSV and read it back as data frame.
# first we convert the dictionary into Pandas data frame
import pandas as pd
dic = {'field1': [3, 2, 1, 0], 'field2': ['a', 'b', 'c', 'd']}
df = pd.DataFrame.from_dict(dic)
df
# to write data frame into CSV
fileName="myData3.csv"
df.to_csv(fileName,index=False)
# to read SCV into dataframe
df = pd.read_csv(fileName)
df
JSON is popular data format, which is very similar to nested dictionary or nested list in Python. Python has json module as part of standard modules.
import json
# Convert Python List to JSON file
lst = [41, 58, 'abc', '78']
filename="myData4.json"
with open(filename, 'w') as fp:
json.dump(lst, fp)
# read JSON file
with open(filename, 'r') as fp:
data = json.load(fp)
print(data)
type(data)
# Convert Python dictionary to JSON file
myDict = {'a':'apple', 'b':'banana', 'c':'cherry'}
filename="myData5.json"
with open(filename, 'w') as fp:
json.dump(myDict, fp)
# read JSON file
with open(filename, 'r') as fp:
data = json.load(fp)
print(data)
type(data)
Let us use the previous dataframe
df
# from pandas data frame into json file
filename="myData6.json"
df.to_json(filename)
# from json into pandas data frame
df=pd.read_json (filename)
df