| Mode | Details |
|---|---|
| read() | Reading content of a file |
| readable() | Check if file is readable or not |
| write() | Write content to file |
| seek() | Move the file pointer to different location inside the file |
| seekable() | Check if file is seekable or not |
| tell() | Get the position of the file pointer |
| readlines() | Getting a list from the content of a file |
| readline() | Reading line of data from a file |
| truncate() | Resize a file upto the input bytes |
| writable() | Check if the file is writable or not |
| writelines() | Writing list to file |
names=[]
path="D:\\my_data\\student.csv" # sample CSV file, use your path
fob=open(path,)
headings = next(fob) # removing header row
for rec in fob:
student=rec.split(',')
print(student)
names.append(student[1]) # name added
Using csv library we can create a list.
import csv
file = open('D:\student.csv') # Path of CSV data file, use your path
csvreader = csv.reader(file)
l1 = []
l1 = next(csvreader) # column headers as first row
r_set = [row for row in csvreader]
path='D:/my_dir1/plus2net/python/list.php' # path of file to update
fob=open(path,'r') # Open in read mode
data=fob.read() # collect data
fob.close() # close file object
#print(data)
data=data.replace('\n\n','\n') # replace double line break with single
#print(data)
with open(path, "w") as f: # Open in Write mode
f.write(data) # rewrite data
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.