文件的读写原理(2)
(一)文件对象的常用方法
read([size]) :从文件中读取size个字节或字符的内容返回,若省略size,则读取到文件末尾readline() :从文本文件中读取一行字符readlines() :把文本文件中每一行都作为独立的字符串对象,并将这些对象放入列表返回write(str) :将字符串str写入文件writelines(s_list) :将字符串列表s_list写入文本文件,不添加换行符seek(offset[,where]) :把文件指针移动到新的位置,offset表示相对于where的位置
offset:为正往结束方向移动,为负往开始方向移动 where(0):从文件头开始结算(默认值) where(1):从当前位置开始计算 where(2):从文件尾开始计算
7.tell() :返回文件指针的当前文件 8.flush() :把缓冲区的内容写入文件,但不关闭文件 9.close() :把缓冲区的内容写入文件,同时关闭文件,释放文件中相关资源
file= open ( 'a.txt' , 'r' )
#print(file.read(2)) #读取两个字符 世界
#print(file.readline()) #读取一行 世界
#print(file.readlines()) #读取全部内容,以列表方式输出 ['世界\n', '中国']
file= open ( 'a.txt' , 'a' )
#file.write('hello')
lst= [ 'java' , 'python' , 'go' ]
file. writelines ( lst)
file. close ( )
file= open ( 'a.txt' , 'r' )
file. seek ( 2 )
print ( file. read ( ) )
file. close ( )
界
中国javapythongo
Process finished with exit code 0
file= open ( 'a.txt' , 'r' )
file. seek ( 2 )
print ( file. read ( ) )
print ( file. tell ( ) )
file. close ( )
界
中国javapythongo
22
Process finished with exit code 0
(二)with语句(上下文管理器)
with语句可以自动管理上下文资源,不论什么原因跳出with块,都能确保文件正确的关闭,以此达到资源释放的目的 语法: with open(‘filename’,‘file_op’) as src_file:
with open ( 'logo.png' , 'rb' ) as src_file:
with open ( 'copy2logo.png' , 'wb' ) as target_file:
target_file. write ( src_file. read ( ) )
(三)OS模块的常用函数
OS模块是Python内置的与操作系统功能和文件系统功能相关的模块,该模块中的语句的执行结果通常与操作系统有关,在不同的操作系统上运行,得到的结果可能不一样 OS模块与os.path模块用于对目录或文件进行操作
#os模块是与操作系统相关的一个模块
import os
#os.system('notepad.exe') #打开记事本
#os.system('calc.exe') #打开计算器
#直接调用可执行文件
os. startfile ( 'C:\\Program Files (x86)\\Tencent\\QQ\\Bin\\qq.exe' )
OS模块操作目录相关函数 1.getwd() :返回当前的工作目录 2.listdir() :返回指定路径下的文件和目录信息 3.mkdir(path[,mode]) :创建目录 4.makedirs(path1/path2…) :创建多级目录 5.rmdir() :删除目录 6.removedirs() :删除多级目录 7.chdir(path) :将path设置为当前工作目录
import os
print ( os. getcwd ( ) )
lst= os. listdir ( '../chap15' ) #返回该路径下的全部文件和目录信息
print ( lst)
#os.mkdir('new_dir2')
#os.makedirs('A/B/C') #创建多级目录
#os.rmdir('new_dir2') #删除目录
#os.removedirs('A/B/C')
(四)os.path模块
1.abspath(path) :用于获取文件或目录额绝对路径 2.exist(path) :用于判断文件或目录是否还存在,如果存在则返回True,否则返回False 3.join(path,name) :将目录与目录或文件名连接起来 4.splitext() :分离文件名和扩展名 5.basename(path) :从一个目录中提取文件名 6.dirname(path) :从一个路径中提取文件路径,不包括文件名 7.isdir(path) :用于判断是否为路径
import os. path
print ( os. path. abspath ( 'demo13.py' ) )
print ( os. path. exists ( 'demo3.py' ) , os. path. exists ( 'demo18.py' ) ) #True False
print ( os. path. join ( 'E:\\python' , 'demo13.py' ) ) #E: \python\demo13. py
print ( os. path. split ( 'E:\\vippython\\chap15\\demo13.py' ) ) #( 'E:\\vippython\\chap15' , 'demo13.py' )
print ( os. path. splitext ( 'demo13.py' ) ) #( 'demo13' , '.py' )
print ( os. path. basename ( 'E:\\vippython\\chap15\\demo13.py' ) ) #demo13. py
print ( os. path. dirname ( 'E:\\vippython\\chap15\\demo13.py' ) ) #E: \vippython\chap15
print ( os. path. isdir ( 'E:\\vippython\\chap15\\demo13.py' ) ) #False
#列出指定目录下的所有py文件
import os
path= os. getcwd ( )
lst= os. listdir ( path)
for filename in lst:
if filename. endswith ( '.py' ) :
print ( filename)
demo1. py
demo10. py
demo11. py
demo12. py
demo13. py
demo14. py
demo15. py
demo16. py
demo2. py
demo3. py
demo4. py
demo5. py
demo6. py
demo7. py
demo8. py
demo9. py
__init__. py
Process finished with exit code 0
import os
path= os. getcwd ( )
lst_files= os. walk ( path)
for dirpath, dirname, filename in lst_files:
print ( dirpath)
print ( dirname)
print ( filename)
print ( '----------------------------------------' )
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15
[ 'new_dir' ]
[ 'a.txt' , 'b.txt' , 'copy2logo.png' , 'copylogo.png' , 'demo1.py' , 'demo10.py' , 'demo11.py' , 'demo12.py' , 'demo13.py' , 'demo14.py' , 'demo15.py' , 'demo16.py' , 'demo17.py' , 'demo2.py' , 'demo3.py' , 'demo4.py' , 'demo5.py' , 'demo6.py' , 'demo7.py' , 'demo8.py' , 'demo9.py' , 'logo.png' , 'logp.png' , '__init__.py' ]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15\new_dir
[ 'sub' ]
[ '1.py' , '2.py' ]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15\new_dir\sub
[ ]
[ 'sub1.py' ]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Process finished with exit code 0
import os
path= os. getcwd ( )
lst_files= os. walk ( path) #遍历指定目录下的所有文件和目录
for dirpath, dirname, filename in lst_files:
for dir in dirname:
print ( os. path. join ( dirpath, dir) )
for file in filename:
print ( os. path. join ( dirpath, file) )
C: \Users\asus\Desktop\Education_Python\venv\Scripts\python. exe C: / Users/ asus/ Desktop/ Education_Python/ venv/ 基础语法/ chap15/ demo17. py
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15\new_dir
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15\a. txt
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15\b. txt
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15\copy2logo. png
C: \Users\asus\Desktop\Education_Python\venv\基础语法\chap15\copylogo. p
. . . . . . . . . . . . . . . . . . .