文件操作
- 写入文件
# 写 write
f = open('aa.txt','a')
f.write('\n hhh')
f.close()
- 打开文件
open()
========= ===============================================================
Character Meaning
--------- ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
========= ===============================================================
with open('aa.txt') as f:
if f.readable(): # 文件是否可读
# t = f.read()
line = f.readline()
print(line)
-
复制文件
- 源文件 – 读
- 目标文件 – 写
-
多个文件操作 :
os.path
> ntpath.py
import os
# os.path.*
os.path.isabs() # 是否绝对路径
os.path.isdir
os.path.isfile()
os.path.dirname(__file__)
os.path.abspath()
os.path.split() # 分离 文件名
os.path.splitext() # 分离 扩展名
os.path.getsize() # 获取文件大小 字节
os
import os
import os
# o 操作 s 系统
os.getcwd() # 获取当前目录
os.listdir() # ls -> 列表
os.mkdir()
os.rmdir() # 仅删除 空文件夹
os.removedirs() # 删除多个 文件夹
os.remove() # 删文件
os.chdir() # 切换当前 工作目录
os.chmod() #