python 核心编程 第九章

    xiaoxiao2021-03-25  91

    第1题 #!/usr/bin/env pythonprint "Please Input The Name Of File:",FileName = raw_input()files = open(FileName,"r")i = 0for Line in files: if Line[0] == "#" and i != 0: pass else: print Line, i = i + 1

    第2题

    #!/usr/bin/env pythonprint "PLease Input The Name Of File:",FileName = raw_input()print "Please Input How Many Lines Do You Want To Show:",Lines = int(raw_input())i = 0Fs = open(FileName,"r")for L in Fs: print L, i += 1 if i == Lines: break

    第3题

    #!/usr/bin/env pythonprint "Please Input The Name Of File:",NM = raw_input()Fs = open(NM,"r")i = 0for L in Fs: i += 1Fs.close()print "%d"%i

    第4题

    #!/usr/bin/env pythonShowLine = 25def Show25(fs): i = 0 for L in fs: print L, i += 1 if i == ShowLine: breakprint "Please Input The Name Of The File:",NM = raw_input()Fs = open(NM,"r")Show25(Fs)while True: print "Entry Any Key To GoOn,\"q\" To Quit", ch = raw_input() if ch == 'q': break else: Show25(Fs)

    第6题

    #!/usr/bin/env pythonimport sysF1 = open(sys.argv[1],"r")F2 = open(sys.argv[2],"r")db1 = F1.readlines()db2 = F2.readlines()i = 0while True: if db1[i] == None or db2[i] == None: break if db1[i] != db2[i]: break else: i += 1print i+1

    第7题

    #!/usr/bin/env pythonNM = "/etc/serves"Fs = open(NM,"r")print ([L for L in Fs])

    第13题

    #!/usr/bin/env pythonimport sysfor V in sys.argv: print V

    第15题

    #!/usr/bin/env pythonimport sysF1 = open(sys.argv[1],"r")F2 = open(sys.argv[2],"w")for L in F1: F2.writelines(L)F1.close()F2.close()

    第16题

    #!/usr/bin/env pythonimport sysimport osLen = 80F1 = open(sys.argv[1],"r+")F1.seek(0,2) # get lenght of fileLs = F1.tell()indexs = LenHasWrite = 0 #the number of char that has write in filewhile True: if indexs >= Ls: break F1.seek(indexs+HasWrite,0) F1.write(os.linesep) HasWrite += 1 indexs += LenF1.close()

    第17题

    #!/usr/bin/env pythonimport osdef CreateFile(): print "Please Input The Name Of File:", NM = raw_input() if os.path.exists(NM): #Get name and be sure the file is exist or not print "%s Is Existing,Do You Want To Cover Old File?(Y/N)", ANS = raw_input() if ANS == 'Y': pass else: return F1 = open(NM,"a") print "Please Input The Words:", strs = raw_input() F1.write(strs) F1.close() return NMdef ShowFile(): print "Which File Do You Want To Show:", names = raw_input() if os.path.exists(names): F = open(names,"r") for L in F: print L, else: print "%s Don't Exist."%namesdef EditFile(): print "Which File Do You Want To Edit:", names = raw_input() if not os.path.exists(names): print "%s Don't Exist."%names return print "Please Input The Index Of The Line That's Your Want To Edit:", indexs = int(raw_input()) F = open(names,"r") db = [] for L in F: db.append(L) F.close() print "Please Input The Words:", strs = raw_input() if len(db) >= (indexs-1): db[indexs-1] = strs #edit else: print "Out Of Index's Range." return F = open(names,"w") for lines in db: F.writelines(lines) F.close()def SaveFile(): print "Which File Do You Want To Close:", names = raw_input() try: F = open(names) except Exception: F.close() else: passprint "A:Create B:Show C:Edit D:Save E:Quit"Choice = raw_input()if Choice == 'A': NMs = CreateFile()if Choice == 'B': ShowFile()if Choice == 'C': EditFile()if Choice == 'D': SaveFile()print "Bye" # entry E or other

    第18题

    #!/usr/bin/env pythonprint "Please Input The Name Of File:",NM = raw_input()print "Please Input The Any Char:",chs = raw_input()F = open(NM,"r")db = []for line in F: db.append(line)F.close()Cs = 0for L in db: Cs += L.count(chs)print Cs

    第19题 #!/usr/bin/env pythonimport sysimport stringimport randomss = string.letters[:26] + string.letters[27:]def CreateBigFile(ch = 'A', times = 99 , size = 100000): File = open("s.txt","w") Counts = 0 for i in range(size): if random.randint(0,size/times) == 1 and Counts < times: File.write(ch) Counts += 1 else: File.write(ss[random.randint(0,len(ss)-1)]) if Counts < times: for i in range(times-Counts): File.write(ch) File.close()if __name__ == "__main__": CreateBigFile()

    转载请注明原文地址: https://ju.6miu.com/read-15475.html

    最新回复(0)