open(file, mode='r')
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
>>> f = open("D:\poem.txt","r",encoding = "UTF-8")
>>> f.read()
'锄禾\n锄禾日当午,\n汗滴禾下土,\n谁知盘中餐,\n粒粒皆辛苦。'
>>> f.close()
>>> f = open("D:\poem.txt","r",encoding = "UTF-8")
>>> f.read()
'锄禾\n锄禾日当午,\n汗滴禾下土,\n谁知盘中餐,\n粒粒皆辛苦。'
>>> f.close()
>>> f.read()
#ValueError: read of closed file
size
为从文档中读取的字符数>>> f = open("D:\poem.txt","r",encoding = "UTF-8")
>>> f.read()
'锄禾\n锄禾日当午,\n汗滴禾下土,\n谁知盘中餐,\n粒粒皆辛苦。'
>>> f.close()
>>> f = open("D:\poem.txt","r",encoding = "UTF-8")
>>> f.readline()
'锄禾\n'
>>> f.readline()
'锄禾日当午,\n'
>>> f.readline()
'汗滴禾下土,\n'
>>> f.readline()
'谁知盘中餐,\n'
>>> f.readline()
'粒粒皆辛苦。'
>>> f.close()
>>> f = open("D:\poem.txt","r",encoding = "UTF-8")
>>> line = f.readlines()
>>> for i in range(len(line)):
print(line[i])
结果
锄禾
锄禾日当午,
汗滴禾下土,
谁知盘中餐,
粒粒皆辛苦。
>>> f = open("D:\poem.txt","r",encoding = "UTF-8")
>>> f.readline()
'0123456789abcdef'
>>> f.tell()
16
>>> f.readline()
''
>>> f.tell()
16 #文档当前行最后一个字符位置
>>> f.write(b'0123456789abcdef')
16
>>> print(f.seek(5))
5
>>> print(f.seek(-3,2))
13
>>> f.read(1)
b'd'
>>> f.close()
>>> f.write(b'0123456789abcdef')
16
>>> print(f.seek(5))
5
>>> print(f.seek(-3,2))
13
>>> f.read(1)
b'd'
>>> f.close()
二进制问题
>>> f = open("D:\poem.txt","b",encoding = "UTF-8")
#ValueError: binary mode doesn't take an encoding argument
在文件关闭前或缓冲区刷新前,字符串内容存储在缓冲区中,这时你在文件中是看不到写入的内容的。
如果文件打开模式带b,那写入文件内容时,str(参数)要用encode方法转为bytes形式,否则报错:TypeError: a bytes-like object is required, not ‘str’。
绝对路径和相对路径
import os,sys
ret = os.access("D:\poem.txt",os.F_OK)
print(ret)
ret = os.access("D:\poem.txt",os.R_OK)
print(ret)
ret = os.access("D:\poem.txt",os.W_OK)
print(ret)
ret = os.access("D:\poem.txt",os.X_OK)
print(ret)
os.F_OK: 作为access()的mode参数,测试path是否存在。
os.R_OK: 包含在access()的mode参数中 , 测试path是否可读。
os.W_OK 包含在access()的mode参数中 , 测试path是否可写。
os.X_OK 包含在access()的mode参数中 ,测试path是否可执行。
import os,sys
path = 'D:\'
retval = os.getcwd()
print(retval) #E:\python源码\Document and OS
os.chdir(path)
print("目录修改成功 : %s" % os.getcwd())
import os
dirs = os.listdir()
for item in dirs:
print(item)
import os
print("目录为: %s" % os.listdir(r'.\E\A'))
os.remove(r'.\E\A\test.txt')
print("目录为: %s" % os.listdir(r'.\E\A'))
import os
print("目录为: %s" % os.listdir(os.getcwd()))
os.rename("test", "test2")
print("重命名成功。")
print("目录为: %s" % os.listdir(os.getcwd()))
import os
path = os.getcwd() + '\\a.py'
a = os.system(r'python %s' % path)
os.system('ipconfig/all') # 检查ip地址
import os
print(os.curdir) # .
print(os.pardir) # ..
print(os.sep) # \
print(os.linesep)
print(os.name) # nt
import os
import time
file = r'.\test.txt'
print(os.path.getsize(file)) # 30
print(os.path.getatime(file)) # 1565593737.347196
print(os.path.getctime(file)) # 1565593737.347196
print(os.path.getmtime(file)) # 1565593797.9298275
print(time.gmtime(os.path.getctime(file)))
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=12, tm_hour=7, tm_min=8, tm_sec=57, tm_wday=0, tm_yday=224, tm_isdst=0)
print(time.localtime(os.path.getctime(file)))
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=12, tm_hour=15, tm_min=8, tm_sec=57, tm_wday=0, tm_yday=224, tm_isdst=0
import os
print(os.path.ismount('D:\\')) # True
print(os.path.ismount('D:\\Test')) # False