首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python文件管理、乱码及对passw

Python文件管理、乱码及对passw

作者头像
py3study
发布2020-01-07 17:45:44
6530
发布2020-01-07 17:45:44
举报
文章被收录于专栏:python3python3

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的乱码问题

blob.png
blob.png
blob.png
blob.png
blob.png
blob.png
blob.png
blob.png

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"))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档