Python small exercise-file operation

Summary: Customize running shortcut keys

I thought it was too troublesome to click with the mouse before. I set it up by the way today. For reference, click here

Exercise 1: First experience of file operation and coding problems

write(a): write the string a to the file
writelines(b): write the string list to the file without adding newline characters

f = open(r"a.txt","w",encoding="utf-8")	#指定utf-8编码
f.write("今天是2020-11-16")
f.close()

Insert picture description here
The encoding and decoding are matched, and the information can be viewed normally

f = open(r"Blank.txt","a",encoding="utf-8")

s = ["Clichong\n","Lawrence\n","Kacura\n"]
ss = "啦啦啦,啦啦啦,我是卖报的小当家"
f.writelines(s)
f.write(ss)
f.close()

Insert picture description here
Use with context manager to manage

s = ["Clichong ","Lawrence ","Kacura "]

with open(r"Blank.txt","w",encoding="utf-8") as myio:
    myio.writelines(s)		#不会自动的换行

Insert picture description here
ps: To solve the console garbled problem, set the encoding to a unified format.
Insert picture description here

Exercise 2: File reading
  1. read([size])
    reads size characters from the file and returns it as the result. If there is no size parameter, the entire file is read.
    Reading to the end of the file will return an empty string.
  2. readline()
    reads a line of content and returns it as a result. Reading to the end of the file will return an empty string.
  3. In the readlines()
    text file, each line is stored in the list as a string, and the list is returned
s = ["Clichong ","Lawrence ","Kacura "]

with open(r"Birthday.txt","r",encoding="utf-8") as myio:
    print(myio.read(4),end="\n\n")
    myio.seek(0)
    print(myio.readline(),end="\n\n")
    myio.seek(0)
    print(myio.readlines(),end="\n\n")
    myio.seek(0)
    print(myio.read())

Insert picture description here

Exercise 3: Add a line number to the end of each line of the text file
with open(r"Blank.txt","r",encoding="utf-8") as myio:
    line = myio.readlines()
    print(list(enumerate(line)))	#其中enumerate函数可以为每一行在开头添加一个行号
    myline = [str(information).rstrip()+" #"+str(number+1)+"\n" for number,information in enumerate(line)]
    print(myline)

with open(r"Blank.txt","w",encoding="utf-8") as myio:
    myio.writelines(myline)

Insert picture description here
Line number added
Insert picture description here

Exercise 4: Copy a file

initial version:

with open(r"Birthday.txt","r",encoding="utf-8") as myfile:
    with open(r"CopyBirthday.txt","w",encoding="utf-8") as mycopyfile:
        while True:
            if myfile.readline():		#由于判断的时候已经读取了文件会造成文件指针的偏移,会丢包
                mycopyfile.write(myfile.readline())
            else:
                break

Improved version:

with open(r"test.txt","r",encoding="utf-8") as myfile:
    with open(r"copytest.txt","w",encoding="utf-8") as mycopyfile:
        while True:
            line = myfile.readline()		#指读取一次,不会丢包
            if line:
                mycopyfile.write(line)
            else:
                break

As you can see, the contents of the two files are exactly the same
Insert picture description here
Insert picture description here

Exercise 5: Serialization and deserialization

We need to save the "memory block data" to the hard disk or transfer it to other computers via the network. At this time, "serialization and deserialization of objects" is needed. The serialization mechanism of objects is widely used in distributed and parallel systems

pickle.dump(obj, file) obj is the object to be serialized, file refers to the stored file
pickle.load(file) reads data from file and deserializes it into an object

import pickle

a1 = "Clichong"
a2 = "Lawrence"
a3 = "Kacura"

with open(r"Blank.txt","wb+") as myfile:    #只能是二进制的,而且不能指定编码形式
    pickle.dump(a1,myfile)
    pickle.dump(a2,myfile)
    pickle.dump(a3,myfile)

    myfile.seek(0)
    print(myfile.read())

with open(r"Blank.txt","rb") as myfile:
    b1 = pickle.load(myfile)
    b2 = pickle.load(myfile)
    b3 = pickle.load(myfile)

    print(b1,b2,b3)

Insert picture description here
Insert picture description here

Exercise 6: Operation of CSV files

csv (Comma Separated Values) is a comma-separated text format, commonly used for data exchange, Excel file and database data import and export.

import csv

info = ['id','name','age','salary']
infos = [['1005','Petty','41','32000'],['1006','Kitty','22','10000']]

with open(r"csvfile.csv","w",encoding="utf-8") as myfile:
    writebuf = csv.writer(myfile)
    writebuf.writerow(info)		#写入一行(标题)
    writebuf.writerows(infos)	#写入多行(数据)
 
with open(r"csvfile.csv","r",encoding="utf-8") as myfile:
    readbuf = csv.reader(myfile)	#创建 csv 对象,它是一个包含所有数据的列表,每一行为一个元素
   # headers = next(readbuf)		#获得列表对象,包含标题行的信息
    print(list(readbuf))
    myfile.seek(0)
    linetxt = [id+"-"+name+"-"+age+"-"+salary+"\n" for id,name,age,salary in readbuf]

    with open(r"Blank.txt", "w", encoding="utf-8") as mywritefile:
        mywritefile.writelines(linetxt)

Successfully read and rewrite data:
Insert picture description here
Insert picture description here

Exercise 7: Directory operations
import os
import os.path

print("#当前工作路径是",os.getcwd())
#os.mkdir("testdir")    #只能创建一次,若已创建则报错
#os.makedirs("音乐/刘德华")   #一次可以创建多个目录
print("#是否是绝对路径",os.path.isabs("myfile06.py"))
print("#是否是目录文件",os.path.isdir("myfile06.py"))
print("#是否是文件",os.path.isfile("myfile06.py"))
print("#文件是否存在",os.path.exists("myfile06.py"))
print("#文件的大小是",os.path.getsize("myfile06.py"))
print("#返回文件的最后访问时间",os.path.getatime("myfile06.py"))  #返回文件的最后访问时间
print("#返回文件的最后创建时间",os.path.getctime("myfile06.py"))  #返回文件的最后修改时间
print("#返回文件的最后修改时间",os.path.getmtime("myfile06.py"))
print("#直接返回路径",os.path.abspath("myfile06.py"))   #直接返回路径
print("#返回目录路劲",os.path.dirname("E:\PyCharm\workspace\myIO\myfile06.py"))   #返回目录路劲
print("#对路径进行切割",os.path.split(os.path.abspath("myfile06.py")))
print("#对路径切割出文件的拓展名",os.path.splitext(os.path.abspath("myfile06.py")))
s = "D:\\"
print("#对多个目录名进行连接形成绝对路径",os.path.join(s,"音乐","刘德华","忘情水.mp4"))

Insert picture description here

Exercise 8: List all the .py files in the specified directory and output the file name
Method 1: Custom function
import os
import os.path

os.chdir("E:\PyCharm\workspace")		#更改当前的工作目录
print(os.getcwd())

def FileList(dirname):					#传入一个目录名,就可以打印出目录下的全部.py文件
    if not os.path.isdir(dirname):		#判断是否为目录文件
        print("Error! Not Dir!")
        return
    else:
        mylist = os.listdir(os.path.abspath(dirname))	#将所有的文件输出一个列表中方便处理
        print(mylist)

        for file in mylist:
            filename,filetext = os.path.splitext(file)	#python的特殊语法,真是太简单了
            if filetext == ".py":
                print(file)

FileList("myIO")

Insert picture description here
As you can see, all .py files are output normally
Insert picture description here

Method 2: Use endswith

This method uses an endswith function of the string to determine the character at the end
Insert picture description here

for file in os.listdir():
    if file.endswith("py"):	#灵活的使用了字符串的函数
        print(file)

Insert picture description here

Method 3: Inferential (one line of code)

Flexible use of deduction can make the code concise

mypyfile = [pyfile for pyfile in os.listdir() if pyfile.endswith("py")]

As shown in the figure, it can be output normally
Insert picture description here

Exercise 9: Test use of walk traversal function

os.walk() method: returns a tuple of 3 elements, (dirpath, dirnames, filenames),

  • dirpath: To list the path of the specified directory
  • dirnames: all folders under the directory
  • filenames: all files in the directory
#测试walk函数

import os
import os.path

#os.chdir("E:\PyCharm\workspace")
print(os.getcwd())

Allfile = os.walk(os.getcwd())

for dirpath,dirnames,filenames in Allfile:
    for dir in dirnames:
        print(os.path.join(dirpath,dir))
    for file in filenames:
        print(os.path.join(dirpath,file))
  #  print(dirpath)
  #  print(dirnames)
  #  print(filenames)

Insert picture description here

Exercise 10: Test the copy and compression of shutil modules
Copy test
#测试shutil模块


import shutil
import os

print(os.getcwd())
os.chdir("E:\PyCharm\workspace\myIO\音乐\刘德华")
shutil.copy("note.txt","copynote.txt")

os.chdir("E:\PyCharm\workspace\myIO")
#不要忽视.这个符号,要写得完整为.txt,.html
shutil.copytree("音乐\刘德华","电影",ignore=shutil.ignore_patterns(".txt",".html"))

Before
Insert picture description here
running: After running:
Insert picture description here

Compression test
  • The first compression method
#把目录下:音乐/刘德华下的全部内容压缩到目录:电影/歌手 中去,格式为zip格式
 shutil.make_archive("电影/歌手","zip","音乐/刘德华")	

Insert picture description here

  • The second compression method
import shutil
import zipfile

#shutil.make_archive("电影/歌手","zip","音乐/刘德华")

myfile = zipfile.ZipFile("音乐/Clichong.zip","w")	#压缩包存放位置
myfile.write("电影/copynote.txt")		#往压缩文件夹中添加的数据
myfile.write("电影/note.txt")
myfile.close()

Insert picture description here

Unzip test
import shutil
import zipfile

#shutil.make_archive("电影/歌手","zip","音乐/刘德华")

'''''''''
myfile = zipfile.ZipFile("音乐/Clichong.zip","w")
myfile.write("电影/copynote.txt")
myfile.write("电影/note.txt")
myfile.close()
'''''''''

myfile2 = zipfile.ZipFile("音乐/Clichong.zip","r")	#要解压缩的文件为Clichong.zip
myfile2.extractall("音乐")		#解压缩的目录
myfile2.close()


Insert picture description here

Exercise 11: Use a recursive method to traverse the entire directory and print it out as a tree
#递归遍历目录

import os

def DirNameSearch(dirpassname,level):

    allname = os.listdir(dirpassname)
    for dirname in allname:
        wholefilename = os.path.join(dirpassname, dirname)
        if os.path.isdir(wholefilename):
            DirNameSearch(wholefilename,level+1)
        else:
            print("|_"+"___"*level,wholefilename)

DirNameSearch("音乐",0)

The directory structure is as follows:
Insert picture description here
Traverse results:
Insert picture description here
Let's test it with the entire directory:

DirNameSearch("E:\PyCharm\workspace\myIO",0)

Insert picture description here

Insert picture description here
It can still be traversed better

Guess you like

Origin blog.csdn.net/weixin_44751294/article/details/109732684