ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-文件读写

2021-05-27 21:52:46  阅读:131  来源: 互联网

标签:文件 sbin python 读写 fd file open Out


在python中可以使用file模块和opne可以打开文件,注意python3中已经不支持file!

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)    ========= ===============================================================
    Character Meaning    --------- ---------------------------------------------------------------    'r'       open for reading (default)  #只读模式打开    'w'       open for writing, truncating the file first   #只写模式打开,写入清先清空文件    'x'       create a new file and open it for writing     #创建一个新文件写    '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)  #    'U'       universal newline mode (deprecated)    ========= ===============================================================

通过实例来测试open的用法

fd = open(r"E:\file", "w")  #以写的方式打开一个文件fd.write("Hello world!")     #写入一条数据Out[22]: 12fd.close()

fd = open(r"E:\file")           #默认是以只读的方式打开文件fd.read()                             ##Out[26]: 'Hello world!'#write与read的进阶fd.readlines()  #返回的是文件中每一行构成的列表fd.readline()    #每次返回一行,可以用来迭代In [5]: fd = open("/etc/passwd")

In [6]: fd.readlines()
Out[6]: 
['root:x:0:0:root:/root:/bin/bash\n', 'bin:x:1:1:bin:/bin:/sbin/nologin\n', 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n', 'adm:x:3:4:adm:/var/adm:/sbin/nologin\n', 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n', 'sync:x:5:0:sync:/sbin:/bin/sync\n', 'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n', 'halt:x:7:0:halt:/sbin:/sbin/halt\n', 'sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin\n']

In [7]: fd.seek(0)   ##把文件句柄指针指向文件起始位置In [8]: fd.readline()  #只读一行Out[8]: 'root:x:0:0:root:/root:/bin/bash\n'#遍历整个文件之一:In [12]: for i in fd:    
   ....:     print i#遍历整个文件之二:In [14]: fd = open("/etc/passwd")

In [15]: while True:
   ....:     line = fd.readline()
   ....:     if not line:
   ....:         break
   ....:     print(line)
   ....: fd.close()#writelines()的用法In [16]: fd = open("message")

In [17]: lines = fd.readlines()

In [18]: fd.close()

In [19]: fd = open("message","w")

In [20]: lines
Out[20]: ['Hello world!\n', 'I have a dream\n', 'Dangerous\n']

In [21]: lines.append("Swift\n")  #在列表中追加一个字符串In [22]: fd.writelines(lines)     #写入列表In [24]: fd.close()

In [31]: fd = open("message")

In [32]: print fd.read()   
Hello world!
I have a dream
Dangerous
Swift

以上默认是使用文本方式打开,在打开文件时使用“b”表示使用二进制的方式打开。

In [33]: fd = open("message")

In [34]: fd.read()      #默认是读取整个文件Out[34]: 'Hello world!\nI have a dream\nDangerous\nSwift\n'In [35]: fd.tell()      #函数打印出当前指针的位置Out[35]: 44In [36]: fd.seek(0)     #函数可以把句柄指针指向指定的位置In [37]: fd.read(5)      #可以传入参数,打印多少个字节Out[37]: 'Hello'
python2与python3的编码格式:

python2中默认使用ASCII编码,python3中使用UNICODE编码。

string------>bytes(encode(encoding="utf-8")) :unicode转为二进制称为编码

bytes------->string(decode(encoding="utf-8")): 二进制转为Unicode称为解码

注意以上只适合于python3

一个关于编码问题的详细解答链接:https://www.cnblogs.com/575dsj/p/7112767.html

 

标签:文件,sbin,python,读写,fd,file,open,Out
来源: https://blog.51cto.com/lovejxs/2823943

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有