Python file management, garbled characters and sorting of passwd files

Source: Internet
Author: User
Tags readfile

1. Read and write Files

Read the file:

f = open ("1.txt") Text = F.readlines () print (text)


Write file:


f = open ("2.txt", ' W ') encoding= ' Utf-8 ' F.write ("Hello world!") F.close ()

Code:

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 ()

Execution process:

Please input the file name:fxq.logPlease input the file Context:hello world! Please input the file Context:my name is fengxiaoqingplease input the file context:i ' m 30Please input the file context: I ' m a bright boy.lplease 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 Fengxiaoqingi ' m 30I ' m a bright boy.lthink you very much!################### END ##################### #进程已结束, Exit generation Code 0

2. File methods

File Common methods:

ReadLine ()

ReadLines ()

Next ()

Read ()

Write () writes a string

The writelines () parameter is a sequence, such as a list, that iterates over the file for you

File properties:

F.name

f.closed

f.encoding

F.mode

With usage:


With Codec.open ("1.log", encoding= "Utf-8") as F:print (F.read ())

The garbled problem of 3.python2


In Python2:

Import Sys

Reload (SYS)

Print (sys.getdefaultencoding ())

4.python Sorting of passwd files

Password file:

[[email protected] ~]# cat passwd.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/ sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/ lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/ Sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames : X:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:ftp user:/var/ftp:/sbin/nologinnobody:x:99:99:nobody :/:/sbin/nologinsystemd-bus-proxy:x:999:997:systemd bus proxy:/:/sbin/nologinsystemd-network:x : 192:192:systemd network management:/:/sbin/nologindbus:x:81:81:system message bus:/:/sbin /nologinpolkitd:x:998:996:user for polkitd:/:/sbin/nologinlibstoragemgmt:x:997:995:daemon  account for libstoragemgmt:/var/run/lsm:/sbin/nologinrpc:x:32:32:rpcbind daemon:/var/lib/ Rpcbind:/sbin/nologintss:x:59:59:account used by the trousers package to sandbox the tcsd  daemon:/dev/null:/sbin/nologinntp:x:38:38::/etc/ntp:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/ nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:privilege-separated ssh:/var/empty/ Sshd:/sbin/nologinchrony:x:996:994::/var/lib/chrony:/sbin/nologintcpdump:x:72:72::/:/sbin/nologindockerroot:x : 995:992:docker user:/var/lib/docker:/sbin/nologinsaslauth:x:994:76:saslauthd user:/run/saslauthd:/ Sbin/nologinmailnull:x:47:47::/var/spool/mqueue:/sbin/nologinsmmsp:x:51:51::/var/spool/mqueue:/sbin/nologin

code:

#/usr/bin/env python# -*- coding:utf-8 -*-#  @time    :2018/1/25  22:11#  @Author  :FengXiaoqing#  @file    :uidsort-passwd.pyimport codecsfile  =  "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"))


Python file management, garbled characters and sorting of passwd files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.