标签:__name__ etc pen think local /dev/null cer lib inpu
1.读写文件读文件:
f = open("1.txt") text = f.readlines() print(text)
写文件:
f = open("2.txt",'w')encoding='utf-8' f.write("Hello World!") f.close()
代码:
class File(): ENCODING = "utf-8" def wirteFile(self): filename = input("Please input the file name: ") f = codecs.open(filename,'w',encoding=File.ENCODING) while 1: context = input("Please input the file context: ") if context == "EOF": break f.write(context) f.write("\n") def readFile(self): print("####################STAT######################") readfile = input("Please input your read file name: ") fileread = codecs.open(readfile,encoding=File.ENCODING) readContext = fileread.read() print(readContext) print("################### END ######################") fileread.close() if __name__ == '__main__': import codecs File = File() File.wirteFile() File.readFile()
执行过程:
Please input the file name: fxq.log Please input the file context: Hello world! Please input the file context: My name is Fengxiaoqing Please input the file context: I'm 30 Please input the file context: I'm a bright boy.l Please input the file context: Think you very much! Please input the file context: EOF ####################STAT###################### Please input your read file name: fxq.log Hello world! My name is Fengxiaoqing I'm 30 I'm a bright boy.l Think you very much! ################### END ###################### 进程已结束,退出代码0
2. 文件方法
文件常用方法:
readline()
readlines()
next()
read()
write() 写入的是字符串
writelines() 参数是序列,比如列表,它会迭代帮你 写入文件
文件属性:
f.name
f.closed
f.encoding
f.mode
with用法:
with codec.open("1.log",encoding= "utf-8") as f: print(f.read())
3.python2的乱码问题
python2中:
import sys
reload(sys)
print(sys.getdefaultencoding())
4.python对passwd文件进行排序
密码文件:
[root@localhost ~]# cat passwd.txt 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 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin libstoragemgmt:x:997:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:996:994::/var/lib/chrony:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin dockerroot:x:995:992:Docker User:/var/lib/docker:/sbin/nologin saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
代码:
#/usr/bin/env python # -*- coding:utf-8 -*- # @time :2018/1/25 22:11 # @Author :FengXiaoqing # @file :uidSort-Passwd.py import codecs file = "passwd.txt" sortfile = "sortpasswd.txt" fileContext = [] sortuid = [] with codecs.open(sortfile,'wb') as fsort: with codecs.open(file,encoding='utf-8') as f: fileContext += f.readlines() for line in fileContext: sortuid.append(int(line.split(':')[2])) sortuid.sort() for uid in sortuid: for line in fileContext: if str(uid) == line.split(":")[2]: print(line) fsort.write(line.encode("utf-8"))
标签:__name__ etc pen think local /dev/null cer lib inpu
原文地址:http://blog.51cto.com/fengyunshan911/2065251