Python文件管理、乱码及对passwd文件排序

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文件进行排序

密码文件:

[[email protected] ~]# 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"))

原文地址:http://blog.51cto.com/fengyunshan911/2065251

时间: 2024-08-06 00:03:19

Python文件管理、乱码及对passwd文件排序的相关文章

读写文件、文件方法、python2的乱码问题、python对passwd文件进行排序

读写文件 if __name__=='__main__': filename=input('请输入保存的文件:') fdfile=open(filename,'w+') while 1: text=input('请输入内容: ') if text == 'EOF': break else: fdfile.write(text) fdfile.write('\n') fdfile.close() readfile=open(filename) print('##############start#

python logging模块写入中文,文件乱码的解决方法

一下文件执行将产生乱码,切.log文件显示问好,打不开 import logging def shop_logging(name): name = name+"登录成功!" logger = logging.getLogger() fh = logging.FileHandler("test.log") formatter = logging.Formatter("%(asctime)s - %(name)s-%(levelname)s %(message

文件排序去重新思路

我一直觉得思路是比较重要的. 前几天终于从一个充满了垃圾广告的网盘里下载到了传说中的 csdn 数据库(就是以前泄露的那个,现在被各种封杀了) 我写了个一个python脚本从中提取密码(用户名和邮箱我都不需要),用来做一个字典. 但是我发现,其中有很多是重复的,比如123456789之类的. 所以,第一个问题摆在了我的面前,怎么去重,怎么给文件中的数据去重. 我首先想到的就是python 中的先set在list. 然后我遇到了第二个问题,当这个字典文件比较大的时候,全部读取到内存是不合理的. 通

Python抓取网页&批量下载文件方法初探(正则表达式+BeautifulSoup) (转)

Python抓取网页&批量下载文件方法初探(正则表达式+BeautifulSoup) 最近两周都在学习Python抓取网页方法,任务是批量下载网站上的文件.对于一个刚刚入门python的人来说,在很多细节上都有需要注意的地方,以下就分享一下我在初学python过程中遇到的问题及解决方法. 一.用Python抓取网页 基本方法: [python] view plaincopyprint? import urllib2,urllib url = 'http://www.baidu.com' req 

Python中把数据存入csv文件

with open("可乐数据.csv","a",encoding="gbk") as fp: fp.write("%s,%s,%s,%s"%(result[0],result[1],name.split("%")[1],result[2])+"\n") 这里的csv文件如果在Python解释器中打开的话是乱码的,把文件复制出来用excel打开就正常了. 原文地址:https://www

文件排序合并

文件排序: sort命令是帮我们根据不同的数据类型进行排序.其语法及经常使用參数格式: sort [选项][输入文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序. 參 数: -b 忽略每行前面開始出的空格字符. -c 检查文件是否已经依照顺序排序. -f 排序时.忽略大写和小写字母. -M 将前面3个字母按照月份的缩写进行排序. -n 按照数值的大小排序. -o<输出文件> 将排序后的结果存入指定的文件. -r 以相反的顺序来排序. -t<分隔字符> 指定排序时所用

MYSQL 磁盘临时表和文件排序

因为Memory引擎不支持BOLB和TEXT类型,所以,如果查询使用了BLOB或TEXT列并且需要使用隐式临时表,将不得不使用MyISAM磁盘临时表,即使只有几行数据也是如此. 这会导致严重的性能开销..即使配置Mysql将临时表存储在内存块设备上(ram-disk),依然需要很多昂贵的系统调用. 最好的解决方案是尽量避免使用BLOB和TEXT类型.如果实在无法避免,有一个技巧是在所有用到BLOG字段的地方都使用SUBSTRING(culumn,length)将列值转换为字符串(在order b

Linux文件排序和FASTA文件操作

文件排序 seq: 产生一系列的数字; man seq查看其具体使用.我们这使用seq产生下游分析所用到的输入文件. # 产生从1到10的数,步长为1 $ seq 1 10 1 2 3 4 5 6 7 8 9 10 # 产生从1到10的数,步长为1,用空格分割 $ seq -s ' ' 1 10 1 2 3 4 5 6 7 8 9 10 # 产生从1到10的数,步长为2 # 如果有3个数,中间的数为步长,最后一个始终为最大值 $ seq -s ' ' 1 2 10 1 3 5 7 9 $ cat

LINUX用户管理——/etc/passwd文件详解

2010-09-21 12:13:41|  分类: LINUX|举报|字号 订阅 下载LOFTER客户端 输入vi /etc/passwd 可以查看此文件的内容 .本机内容如下: [root@localhost ~]# vi /etc/passwdroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/