Python File I/O

File is a named location ondisk to store related information. It is used to permanently storedata in a non-volatile memory (e.g. hard disk). Since, random accessmemory (RAM) is volatile which loses its data when computer isturned off, we use files for future use of the data.

When we want to read fromor write to a file we need to open it first. When we are done, itneeds to be closed, so that resources that are tied with the fileare freed. Hence, in Python, a file operation takes place in thefollowing order.

  1. Open a file

  2. Read or write (performoperation)

  3. Close the file

  1. Opening a File

Python has a built-infunction open()to open a file. This function returns a file object, also called ahandle, as it is used to read or modify the file accordingly.

	
	>>> f =
	open("test.txt")    # open file in current directory
	>>> f =
	open("C:/Python33/README.txt")  # specifying full path

We can specify the modewhile opening a file. In mode, we specify whether we want to read'r', write 'w' or append 'a' to the file. We also specify if we wantto open the file in text mode or binary mode. The default is readingin text mode. In this mode, we get strings when reading from thefile. On the other hand, binary mode returns bytes and this is themode to be used when dealing with non-text files like image or exefiles.

PythonFile Modes

Mode

Description

'r'

Open a file for reading.(default)

'w'

Open a file for writing.Creates a new file if it does not exist or truncates the file ifit exists.

'x'

Open a file forexclusive creation. If the file already exists, the operationfails.

'a'

Open for appending atthe end of the file without truncating it. Creates a new file ifit does not exist.

't'

Open in text mode.(default)

'b'

Open in binary mode.

'+'

Open a file for updating(reading and writing)

	
	f = open("test.txt")
	     # equivalent to 'r' or 'rt'
	f = open("test.txt",'w')
	 # write in text mode
	f =
	open("img.bmp",'r+b') # read and write in binary mode

Since the version 3.x,Python has made a clear distinction between str(text) and bytes(8-bits). Unlike other languages, the character 'a' does not implythe number 97 until it is encoded using ASCII(or other equivalent encodings). Hence, when working with files intext mode, it is recommended to specify the encoding type. Files arestored in bytes in the disk, we need to decode them into strwhen we read into Python. Similarly, encoding is performed whilewriting texts to the file.

The default encoding isplatform dependent. In windows, it is 'cp1252' but 'utf-8' in Linux.Hence, we must not rely on the default encoding otherwise, our codewill behave differently in different platforms. Thus, this is thepreferred way to open a file for reading in text mode.

	
f
	= open("test.txt",mode = 'r',encoding = 'utf-8')
    1. Closing a File

When we are done withoperations to the file, we need to properly close it. Python has agarbage collector to clean up unreferenced objects. But we must notrely on it to close the file. Closing a file will free up theresources that were tied with the file and is done using the close()method.

	
	f =
	open("test.txt",encoding = 'utf-8')
	# perform file
	operations
	f.close()

This method is not entirelysafe. If an exception occurs when we are performing some operationwith the file, the code exits without closing the file. A safer wayis to use a try...finallyblock.

	
	try:
	   f
	= open("test.txt",encoding = 'utf-8')
	   #
	perform file operations
	finally:
	   f.close()

This way, we are guaranteedthat the file is properly closed even if an exception is raised,causing program flow to stop.

The best way to do this isusing the withstatement. This ensures that the file is closed when the blockinside withis exited. We don't need to explicitly call the close()method. It is done internally.

	
	with
	open("test.txt",encoding = 'utf-8') as f:
	   #
	perform file operations
    1. Writing to a File

In order to write into afile we need to open it in write 'w', append 'a' or exclusivecreation 'x' mode. We need to be careful with the 'w' mode as itwill overwrite into the file if it already exists. All previous dataare erased.

Writing a string orsequence of bytes (for binary files) is done using write()method. This method returns the number of characters written to thefile.

	
	with
	open("test.txt",'w',encoding = 'utf-8') as f:
	   f.write("my
	first file\n")
	   f.write("This
	file\n\n")
	   f.write("contains
	three lines\n")

This program will create anew file named 'test.txt' if it does not exist. If it does exist, itis overwritten. We must include the newline characters ourselves todistinguish different lines.

    1. Reading From a File

To read the content of afile, we must open the file in reading mode. There are variousmethods available for this purpose. We can use the read(size)method to read in sizenumber of data. If sizeparameter is not specified, it reads and returns up to the end ofthe file.

	
	>>> f =
	open("test.txt",'r',encoding = 'utf-8')
	>>> f.read(4)  
	 # read the first 4 data
	'This'
	


	
	>>> f.read(4)  
	 # read the next 4 data
	' is '
	


	
	>>> f.read()   
	 # read in the rest till end of file
	'my first file\nThis
	file\ncontains three lines\n'
	


	
	>>> f.read()  #
	further reading returns empty sting
	''

We can see, that read()method returns newline as '\n'. Once the end of file is reached, weget empty string on further reading. We can change our current filecursor (position) using the seek()method. Similarly, the tell()method returns our current position (in number of bytes).

	
	>>> f.tell()   
	# get the current file position
	56
	


	
	>>> f.seek(0)  
	# bring file cursor to initial position
	0
	


	
	>>>
	print(f.read())  # read the entire file
	This is my first file
	This file
	contains three lines

We can read a fileline-by-line using a forloop. This is both efficient and fast.

	
	>>> for line in
	f:
	...     print(line, end
	= '')
	...
	This is my first file
	This file
	contains three lines

The lines in file itselfhas a newline character '\n'. Moreover,the print()function also appends a newline by default. Hence, we specify theendparameter to avoid two newlines when printing.

Alternately, we can usereadline()method to read individual lines of a file. This method reads a filetill the newline, including the newline character.

	
	>>>
	f.readline()
	'This is my first
	file\n'
	


	
	>>>
	f.readline()
	'This file\n'
	


	
	>>>
	f.readline()
	'contains three lines\n'
	


	
	>>>
	f.readline()
	''

Lastly, the readlines()method returns a list of remaining lines of the entire file. Allthese reading method return empty values when end of file (EOF) isreached.

	
	>>>
	f.readlines()
	['This is my first
	file\n', 'This file\n', 'contains three lines\n']
    1. Python File Methods

There are various methodsavailable with the file object. Some of them have been used in aboveexamples. Here is the complete list of methods in text mode with abrief description.

PythonFile Methods

Method

Description

close()

Close an open file. Ithas no effect if the file is already closed.

detach()

Separate the underlyingbinary buffer from the TextIOBaseand return it.

fileno()

Return an integer number(file descriptor) of the file.

flush()

Flush the write buffer ofthe file stream.

isatty()

Return Trueif the file stream is interactive.

read(n)

Read atmost ncharacters form the file. Reads till end of file if it is negativeor None.

readable()

Returns Trueif the file stream can be read from.

readline(n=-1)

Read and return one linefrom the file. Reads in at most nbytes if specified.

readlines(n=-1)

Read and return a list oflines from the file. Reads in at most nbytes/characters if specified.

seek(offset,from=SEEK_SET)

Change the file positionto offsetbytes, in reference to from(start, current, end).

seekable()

Returns Trueif the file stream supports random access.

tell()

Returns the current filelocation.

truncate(size=None)

Resize the file stream tosizebytes. If sizeis not specified, resize to current location.

writable()

Returns Trueif the file stream can be written to.

write(s)

Write string sto the file and return the number of characters written.

writelines(lines)

Write a list of linesto the file.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值