美文网首页
Io——文件的操作

Io——文件的操作

作者: __Python__ | 来源:发表于2017-07-11 18:56 被阅读0次

一.常见的模式

二.例子如下

# r模式

file=open('haha.html','r',encoding='gbk')

f=file.read()

print(f)

file.close()

# w模式

file=open('wenjian.txt','w')

file.write('窗前明月光')

file.close()

# a模式

file=open('wenjian.txt','a')

file.write('窗前明月光,疑是地上霜。')

file.close()

# w+模式

file=open('wenjian.txt','w+')

file.write('举头望明月,')

file.seek(0)

a=file.read()

print(a)

file.close()

# r+模式

file=open('wenjian.txt','r+')

file.write('低头思故乡')

file.seek(0)

a=file.read()

print(a)

file.close()

# a+模式

file=open('haha.html','a+')

file.write('长颈鹿的脖子长')

file.seek(0)

a=file.read()

print(a)

file.close()

# rb模式

file=open('haha.html','rb')

a=file.read()

print(a.decode('gbk'))

file.close()

# wb模式

file=open('haha.html','wb')

a='夹着火车上面包'

a=a.encode('gbk')

file.write(a)

file.close()

# ab模式

file=open('haha.html','ab')

a='夹着火车上面包'

a=a.encode('gbk')

file.write(a)

file.close()

# rb+模式

file=open('haha.html','rb+')

a='三人行必有我师'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()

# wb+模式

file=open('haha.html','wb+')

a='三人行必有我师11'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()

'''

# ab+模式

file=open('haha.html','wb+')

a='三人行必有我师111'

a=a.encode('gbk')

file.write(a)

file.seek(0)

file.read()

file.close()

注意:

.write()模式,·如果文件不存在那么创建,如果存在那么就先清空,然后写入数据。

三.路径。a=f.write('hello world, i am here!')  print(a) 这样写返回值是长度。

三.路径问题

在windows下:

四.读数据

(一) readline:

file=open('wenjian.txt','r+')

a=file.write('窗前明月光,\n疑是地上霜,\n')

print(file.readline())

print(file.readline())

file.close()

或者:

file=open('wenjian.txt','r+')

a=file.write('窗前明月光,\n疑是地上霜,\n')

n=1

for i in a :

print(%s:%s)%(n,i)

n+=1

file.close()

(二) readlines:

f = open('test.txt','r')

content = f.readlines()

print(type(content))

i=1

fortempincontent:

print("%d:%s"%(i, temp))

i+=1

f.close()

五.获取文件位置(tell())

f = open("test.txt","r")

str = f.read(3)

position = f.tell()

print("当前文件位置: ", position)

六.定位文件位置(seek())

1.from:方向

a)0:表示文件开头(python3)

b)1:表示当前位置(python2)

c)2:表示文件末尾(python2)

f = open("test.txt","r")

str = f.read(30)

print("读取的数据是: ", str)

#查找当前位置

position = f.tell()

print("当前文件位置: ", position)

#重新设置位置

f.seek(5,0)

#查找当前位置

position = f.tell()

print("当前文件位置: ", position)

f.close()

四.os模块

(一):文件的重命名、删除


(二):创建文件夹

(三)获取当前目录

importos

os.getcwd()

(四)改变默认目录

importos

os.chdir("../")

(五)改变默认目录

importos

os.listdir("./")

(六)删除文件夹

importos

os.rmdir("张三")

import shutil

os.rmtree(‘m’)

五.os.path模块

os.path.abspath(path) #返回绝对路径

os.path.split(path)

将path分割成目录和文件名二元组返回。

>>> os.path.split('c:\\csv\\test.csv')

('c:\\csv', 'test.csv')

>>> os.path.split('c:\\csv\\')

('c:\\csv', '')

os.path.dirname(path)

返回path的目录。其实就是os.path.split(path)的第一个元素。

>>> os.path.dirname('c:\\csv\test.csv')

'c:\\'

>>> os.path.dirname('c:\\csv')

'c:\\'

os.path.exists(path)

如果path存在,返回True;如果path不存在,返回False。

>>> os.path.exists('c:\\')

True

>>> os.path.exists('c:\\csv\\test.csv')

False

os.path.isabs(path)

如果path是绝对路径,返回True。

os.path.isfile(path)

如果path是一个存在的文件,返回True。否则返回False。

>>> os.path.isfile('c:\\boot.ini')

True

>>> os.path.isfile('c:\\csv\\test.csv')

False

>>> os.path.isfile('c:\\csv\\')

False

os.path.isdir(path)

如果path是一个存在的目录,则返回True。否则返回False。

>>> os.path.isdir('c:\\')

True

>>> os.path.isdir('c:\\csv\\')

False

>>> os.path.isdir('c:\\windows\\test.csv')

False

相关文章

  • 文件 io

    文件操作 io读操作 io写操作 复制文件 断点续传 bufio包 func NewReader(rd io.Re...

  • python文件处理、路径处理、序列化和反序列化

    文件IO常用操作 一般说IO操作,指的是文件IO,如果指的是网络IO,会直接说。 把文件存储到磁盘上的这个过程,叫...

  • 用Python实现磁盘IO操作全攻略,让数据流动起来!

    01 文件读写 1. 打开文件 读写文件是最常见的IO操作。Python内置了读写文件的函数,方便了文件的IO操作...

  • Java IO与NIO浅谈

    一、传统IO模式下的文件读取传统的文件IO操作都是调用OS提供的底层标准IO操作读取函数read()、write(...

  • 文件IO操作

    ** 文件IO基本操作 文件打开: fileObj = open(filePath,model='')model:...

  • IO(文件操作)

    今天开始学习IO,我觉得知识点比较琐碎,需要多看多练,特别是要掌握一些常用的模块,例如OS模块,os.path模...

  • 文件IO操作

    知识点说明 一、fopen (1)函数原形 FILE * fopen(const char * path, con...

  • linux高级环境编程-标准IO

    标准IO也是带缓存的IO,它们的操作是围绕流进行,而之前的不带缓存IO操作是围绕文件描述符的,标准IO还是基于文件...

  • html5plusAPP的填坑之路

    1、io操作调用plus.io.resolveLocalFileSystemURL的api根据文件路径读取文件,填...

  • Io——文件的操作

    一.常见的模式 二.例子如下 # r模式 file=open('haha.html','r',encoding='...

网友评论

      本文标题:Io——文件的操作

      本文链接:https://www.haomeiwen.com/subject/brwmhxtx.html