读写文件--passwd排序
2018-02-03 00:00
204 查看
1.读写文件
1.写文件
#!/usr/bin/env python # -*- coding:utf8 -*- # @TIME: 2018/2/3 19:47 # @AUTHOR: Chawn # FILE: fileOption.py if __name__ == '__main__': # 输入文件名 filename = input("plz input the name of file:") # 以写的方式打开文件 f = open(filename,"w") while 1: context = input("plz input the context('EOF' for exit): ") if context == 'EOF': f.close() exit(1) else: # 写入输入的内容 f.write(context)
C:\Users\chawn\PycharmProjects\test\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/test/Day0203/fileOption.py plz input the name of file:1.log plz input the context('EOF' for exit): adfasf plz input the context('EOF' for exit): 14233fqawfF plz input the context('EOF' for exit): 65415ASFfdAS plz input the context('EOF' for exit): eof plz input the context('EOF' for exit): EOF Process finished with exit code 1
执行之后,包里会出现1.log,如果本身就有1.log,会重写1.log
adfasf14233fqawfF65415ASFfdASeof
但是这样和预想的不一样,没有空格,在写入文件后加一个写入空格:
else:
f.write(context) f.write("\n")
再次执行:
C:\Users\chawn\PycharmProjects\test\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/test/Day0203/fileOption.py plz input the name of file:1.log plz input the context('EOF' for exit): dADFwaf plz input the context('EOF' for exit): asf564f5da plz input the context('EOF' for exit): 2322as plz input the context('EOF' for exit): EOF Process finished with exit code 1
dADFwaf asf564f5da 2322as
2.读文件
#!/usr/bin/env python # -*- coding:utf8 -*- # @TIME: 2018/2/3 20:29 # @AUTHOR: Chawn # FILE: fileRead.py # open('1.log','r')里的'r'可以省略不写,不写默认只读 f = open('1.log','r') print('*'*10 + 'start' + '*'*10) print(f.read()) print('*'*10 + 'end' + '*'*10) f.close()
C:\Users\chawn\PycharmProjects\test\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/test/Day0203/fileRead.py
**********start**********
dADFwaf asf564f5da 2322as
**********end**********
Process finished with exit code 0
2.文件的常用操作
文件的常用方法:readline()一行一行的读
readlines()将文件全部读取,输出list,内容是元素
next()
read()
write()写入的是字符串
writelines()参数是序列,比如列表,它会迭代帮你写入
文件属性
f.name
f.closed判断是否关闭
f.encoding
f.mode (a,r,w)追加,写入,读
open文件最好写成这样的形式:
f = open('1.log','r',encoding="utf-8")
举例:
#!/usr/bin/env python # -*- coding:utf8 -*- # @TIME: 2018/2/3 20:29 # @AUTHOR: Chawn # FILE: fileRead.py # open('1.log','r')里的'r'可以省略不写,不写默认只读 f = open('1.log','r',encoding="utf-8") print('*'*10 ) print(f.name) # print('*'*10 ) # print(f.readline()) print('*'*10 ) print(f.readlines()) print('*'*10) f.close()
C:\Users\chawn\PycharmProjects\test\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/test/Day0203/fileRead.py ********** 1.log ********** ['dADFwaf\n', 'asf564f5da\n', '2322as\n'] ********** Process finished with exit code 0
把print(f.readlines())换成print(f.readline())
#!/usr/bin/env python # -*- coding:utf8 -*- # @TIME: 2018/2/3 20:29 # @AUTHOR: Chawn # FILE: fileRead.py # open('1.log','r')里的'r'可以省略不写,不写默认只读 f = open('1.log','r',encoding="utf-8") print('*'*10 ) print(f.name) print('*'*10 ) print(f.readline()) # print('*'*10 ) # print(f.readlines()) print('*'*10) f.close()
C:\Users\chawn\PycharmProjects\test\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/test/Day0203/fileRead.py ********** 1.log ********** dADFwaf ********** Process finished with exit code 0
可以看出,f.readline()是一行一行读,f.readlines()全部读取
另外,因为通常容易忘记关闭文件f.close(),可以with形式:
with open('1.log','r',encoding="utf-8") as f:
为了避免乱码,通常会导入codecs,所以上面的语句还可以改进:
with codecs.open('1.log','r',encoding="utf-8") as f:
3. 对passwd排序
root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync bin:x:1:1:bin:/bin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
给上面的文件按uid大小排序,效果如下:
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync
思路:
取出第三个元素,排序构成一个序列
此时的序列是由小到大排列的,匹配文件中每行第三个数字,如果相同,就写入一个新的文件,这个新的文件就是所要的
#!/usr/bin/env python # -*- coding:utf8 -*- # @TIME: 2018/2/6 10:30 # @AUTHOR: Chawn # FILE: test.py import codecs file = "passwd" sortfile = "sortpasswd.txt" filecontext = [] sortuid = [] # 'wb' 以byte模式写入 with codecs.open(sortfile,'wb') as fsort: with codecs.open(file,encoding="utf-8") as f: # 把文件内容的每行写进list filecontext = f.readlines() for line in filecontext: # 取出第三个字符串,转化为整型,追加到list sortuid.append(int(line.split(":")[2])) # 列表排序 sortuid.sort() for uid in sortuid: for line in filecontext: # 如果列表sortuid的字符串和line里的第三个字符串相同 if str(uid) == line.split(":")[2]: # 写入这一行到fsort # 写的时候转换编码utf-8 fsort.write(line.encode("utf-8"))
执行结果:
相关文章推荐
- 读写文件、文件方法、python2的乱码问题、python对passwd文件进行排序
- C++读写文件并排序
- python---实战日志文件中查找特定字段,文件操作读写、排序、替换、列表、集合
- C++读写文件,vector排序等
- 爪哇国新游记之八----读写文件及数组排序
- Python文件管理、乱码及对passwd文件排序
- 文件读写问题 以及整数排序问题
- C++读写文件并排序
- java.util.Arrays.sort两种方式的排序(及文件读写练习)
- Java文件读写及对象排序
- go示例1:文件读写和排序(快速 冒泡)
- 一次面试碰到的机试题:计数分词器---文件读写,比较排序,计数统计(觉得偏基础,值得关注)
- perl下关于文件读写,hash统计频数并排序的总结
- 一些常用的js读写文件,排序等
- C++文件读写总结
- C++ 读写操作同一个文件的几种方法
- 文件读写
- 文件排序
- 用户配置文件passwd、密码配置文件shadow、用户组配置文件group、用户及用户组管理
- Android - 文件读写操作 总结